Shiro: 一个易上手,设计灵活的权限框架, 经过简单的配置就能实现系统的安全管理.git
从系统的安全角度考虑, 你的系统至少须要实现以下功能:github
上述功能总结起来为两点:web
Shiro给登陆和权限定义了两个专有名词, 分别是认证(authc)和受权(authz).spring
A,B,C三个页面须要登陆用户访问, 其中: C页面须要有权限的用户才能访问. 在Shiro中能够说成只有认证用户才能访问A,B,C页面. 只有经过受权的用户才能访问页面Capache
系统的安全需求已经清晰了, 下面咱们来配置Shiro, 本例中代码只进行基础配置, 深刻内容后续讲到.安全
因为Spring已经一统天下, 本例中的Shiro所有基于Spring进行配置.bash
Spring下的Shiro须要三个类库:session
"org.apache.shiro:shiro-core:1.3.2",
"org.apache.shiro:shiro-web:1.3.2",
"org.apache.shiro:shiro-spring:1.3.2"
复制代码
咱们进行以下配置: 除/page/n外, 全部请求都须要认证(登陆)才能够访问.app
beans {
// Shiro核心配置
shiroFilter(ShiroFilterFactoryBean) {
securityManager = ref("securityManager")
// 配置URL规则
// 有请求访问时Shiro会根据此规则找到对应的过滤器处理
filterChainDefinitionMap = [
"/page/n" : "anon", // /page/n不须要登陆便可访问
"/**": "authc" // 其他全部页面须要认证(authc为认证过滤器)
]
}
// 安全管理器
securityManager(DefaultWebSecurityManager)
}
复制代码
shiroFilter: 定义及配置shiro核心过滤器并交由Spring管理, 须要被Shiro管理的URL在访问时都通过该过滤器处理(下面会在web.xml
进行配置), Shiro是基于过滤器实现的安全框架, 原理是URL与过滤器对应, 当访问URL时找到对应的过滤器, 在过滤器中处理认证和受权. Shiro内置了不少的过滤器, 下面介绍几个经常使用的过滤器:框架
anon: 匿名过滤器, 不进行任何认证和受权的处理, 全部不须要认证和受权(全部人均可以访问的页面)配置该过滤器, 例如: CSS, JS, 图片, 网站首页等. 对应类为org.apache.shiro.web.filter.authc.AnonymousFilter
authc: 表单认证过滤器, Shiro认证最核心的过滤器, 处理认证, 登陆等操做, 全部须要认证才能访问的URL都需配置该过滤器, 后续篇幅会根据源码讲解该过滤器的实现原理及机制. 对应类为org.apache.shiro.web.filter.authc.FormAuthenticationFilter
logout: 登出过滤器, 通常登出的URL须要配置为logout, 当用户点击登出连接时进入该过滤器. 系统的登出操做交由Shiro处理. 对应类为org.apache.shiro.web.filter.authc.LogoutFilter
为方便记忆与配置,上述的过滤器都是简写, 在Shiro中有一个简写和实际过滤器类的对应关系, 具体其余过滤器的简写能够在代码中进行查看. 在org.apache.shiro.web.filter.mgt.DefaultFilter
中
anon(AnonymousFilter.class),
authc(FormAuthenticationFilter.class),
authcBasic(BasicHttpAuthenticationFilter.class),
logout(LogoutFilter.class),
noSessionCreation(NoSessionCreationFilter.class),
perms(PermissionsAuthorizationFilter.class),
port(PortFilter.class),
rest(HttpMethodPermissionFilter.class),
roles(RolesAuthorizationFilter.class),
ssl(SslFilter.class),
user(UserFilter.class);
复制代码
shiroFilter还能够配置:
loginUrl
: 登陆页面请求地址, 默认为/login.jsp, 当访问请求的用户未登陆时Shiro会重定向到该地址让用户进行登陆.successUrl
: 用户直接访问登陆页, 登陆成功后跳转至此地址. 用户访问页面A时跳转至登陆, 登陆成功后会重定向至页面A.unauthorizedUrl
: 未受权页面地址, 未受权的用户页面时, Shiro会重定向到该页面提示用户无访问权限.securityManager
: Shiro安全管理器, 提供Shiro核心的安全管理逻辑, 后续篇幅细讲. 此处默认声明一下便可.上面提到了全部的URL都交由Shiro的过滤器进行处理, 所以须要在web.xml中添加shiro的过滤器
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
复制代码
<filter-class>
是Spring提供的代理类, 后续会讲到, <filter-name>
必定要和Shiro配置文件中的名称相同才能找到对应的过滤器
<!-- Spring Context Listener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<!-- groovy DSL -->
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.GroovyWebApplicationContext
</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- Shiro configuration -->
<param-value>classpath*:/spring-shiro.groovy</param-value>
</context-param>
复制代码
本例代码使用了Spring4新增的Groovy DSL
语法. 此处只是增长了Shiro配置文件. 无其余特殊配置
至此, 一个基于Shiro控制项目认证的示例配置完成, Shiro所有采用配置. 运行项目, 访问/page/n
, 能够正常访问. 访问其余页面会跳转至登陆.
示例代码地址: github.com/atd681/alld…
示例代码项目: atd681-shiro-first