如下都是综合以前的人加上本身的一些小总结html
Apache Shiro是一个强大且易用的Java安全框架,执行身份验证、受权、密码学和会话管理。使用Shiro的易于理解的API,您能够快速、轻松地得到任何应用程序,从最小的移动应用程序到最大的网络和企业应用程序。java
Shiro 主要分为来个部分就是认证和受权,在我的感受来看就是查询数据库作相应的判断而已,Shiro只是一个框架而已,其中的内容须要本身的去构建,先后是本身的,中间是Shiro帮咱们去搭建和配置好的web
我的认为须要看一下其中的一些源码,更有帮助的深刻的去了解Shiro的原理。spring
Subject即主体,外部应用与subject进行交互,subject记录了当前操做用户,将用户的概念理解为当前操做的主体,多是一个经过浏览器请求的用户,也多是一个运行的程序。 Subject在shiro中是一个接口,接口中定义了不少认证授相关的方法,外部程序经过subject进行认证授,而subject是经过SecurityManager安全管理器进行认证受权数据库
SecurityManager即安全管理器,对所有的subject进行安全管理,它是shiro的核心,负责对全部的subject进行安全管理。经过SecurityManager能够完成subject的认证、受权等,实质上SecurityManager是经过Authenticator进行认证,经过Authorizer进行受权,经过SessionManager进行会话管理等。apache
SecurityManager是一个接口,继承了Authenticator, Authorizer, SessionManager这三个接口。api
Authenticator即认证器,对用户身份进行认证,Authenticator是一个接口,shiro提供ModularRealmAuthenticator实现类,经过ModularRealmAuthenticator基本上能够知足大多数需求,也能够自定义认证器。浏览器
Authorizer即受权器,用户经过认证器认证经过,在访问功能时须要经过受权器判断用户是否有此功能的操做权限。spring-mvc
Realm即领域,至关于datasource数据源,securityManager进行安全认证须要经过Realm获取用户权限数据,好比:若是用户身份数据在数据库那么realm就须要从数据库获取用户身份信息。缓存
注意:不要把realm理解成只是从数据源取数据,在realm中还有认证受权校验的相关的代码。
sessionManager即会话管理,shiro框架定义了一套会话管理,它不依赖web容器的session,因此shiro可使用在非web应用上,也能够将分布式应用的会话集中在一点管理,此特性可以使它实现单点登陆。
SessionDAO即会话dao,是对session会话操做的一套接口,好比要将session存储到数据库,能够经过jdbc将会话存储到数据库。
CacheManager即缓存管理,将用户权限数据存储在缓存,这样能够提升性能。
Cryptography即密码管理,shiro提供了一套加密/解密的组件,方便开发。好比提供经常使用的散列、加/解密等功能。
<!-- shiro --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.4.0</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.4.0</version> </dependency>
<!-- shiro 过滤器 start --> <filter> <filter-name>shiroFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> <!-- 设置true由servlet容器控制filter的生命周期 --> <init-param> <param-name>targetFilterLifecycle</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>shiroFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- shiro 过滤器 end -->
如下只是简单的测试
如下都是根据我的的设置和需求改变的。如今数据是死的,运用的时候须要从数据库中获得
/** * @author zhouguanglin * @date 2018/2/26 14:05 */ public class CustomRealm extends AuthorizingRealm { /** * 受权 * @param principalCollection * @return */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { String userName = (String) principalCollection.getPrimaryPrincipal(); List<String> permissionList=new ArrayList<String>(); permissionList.add("user:add"); permissionList.add("user:delete"); if (userName.equals("zhou")) { permissionList.add("user:query"); } SimpleAuthorizationInfo info=new SimpleAuthorizationInfo(); info.addStringPermissions(permissionList); info.addRole("admin"); return info; } /** * 认证 * @param authenticationToken * @return * @throws AuthenticationException */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { String userName = (String) authenticationToken.getPrincipal(); if ("".equals(userName)) { return null; } SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(userName,"123456",this.getName()); return info; } }
这里面都是按照本身的需求去配置的
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--开启shiro的注解--> <bean id="advisorAutoProxyCreator" class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"> <property name="proxyTargetClass" value="true"></property> </bean> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"/> <!--注入自定义的Realm--> <bean id="customRealm" class="com.test.realm.CustomRealm"></bean> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="customRealm"></property> </bean> <!--配置ShiroFilter--> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager"></property> <!--登入页面--> <property name="loginUrl" value="/login.jsp"></property> <!--登入成功页面--> <property name="successUrl" value="/index.jsp"/> <property name="filters"> <map> <!--退出过滤器--> <entry key="logout" value-ref="logoutFilter" /> </map> </property> <!--URL的拦截--> <property name="filterChainDefinitions" > <value> /share = authc /logout = logout </value> </property> </bean> <!--自定义退出LogoutFilter--> <bean id="logoutFilter" class="com.test.filter.SystemLogoutFilter"> <property name="redirectUrl" value="/login"/> </bean> </beans>
securityManager: 这个属性是必须的。
loginUrl: 没有登陆的用户请求须要登陆的页面时自动跳转到登陆页面,不是必须的属性,不输入地址的话会自动寻找项目web项目的根目录下的”/login.jsp”页面。
successUrl: 登陆成功默认跳转页面,不配置则跳转至”/”。若是登录前点击的一个须要登陆的页面,则在登陆自动跳转到那个须要登陆的页面。不跳转到此。
unauthorizedUrl: 没有权限默认跳转的页面。
过滤器名称 | 过滤器类 | 描述 |
anon | org.apache.shiro.web.filter.authc.AnonymousFilter | 匿名过滤器 |
authc | org.apache.shiro.web.filter.authc.FormAuthenticationFilter | 若是继续操做,须要作对应的表单验证不然不能经过 |
authcBasic | org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter | 基本http验证过滤,若是不经过,跳转屋登陆页面 |
logout | org.apache.shiro.web.filter.authc.LogoutFilter | 登陆退出过滤器 |
noSessionCreation | org.apache.shiro.web.filter.session.NoSessionCreationFilter | 没有session建立过滤器 |
perms | org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter | 权限过滤器 |
port | org.apache.shiro.web.filter.authz.PortFilter | 端口过滤器,能够设置是不是指定端口若是不是跳转到登陆页面 |
rest | org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter | http方法过滤器,能够指定如post不能进行访问等 |
roles | org.apache.shiro.web.filter.authz.RolesAuthorizationFilter | 角色过滤器,判断当前用户是否指定角色 |
ssl | org.apache.shiro.web.filter.authz.SslFilter | 请求须要经过ssl,若是不是跳转回登陆页 |
user | org.apache.shiro.web.filter.authc.UserFilter | 若是访问一个已知用户,好比记住我功能,走这个过滤器 |
在spring中直接引入<import resource="spring-shiro.xml"></import>
<!-- 未认证或未受权时跳转必须在springmvc里面配,spring-shiro里的shirofilter配不生效 --> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <!--表示捕获的异常 --> <prop key="org.apache.shiro.authz.UnauthorizedException"> <!--捕获该异常时跳转的路径 --> /403 </prop> <!--表示捕获的异常 --> <prop key="org.apache.shiro.authz.UnauthenticatedException"> <!--捕获该异常时跳转的路径 --> /403 </prop> </props> </property> </bean>
403是错误页面
@RequestMapping(value = "/login", method = RequestMethod.POST) public String login(String userName, String passwd, Model model) { Subject subject = SecurityUtils.getSubject(); UsernamePasswordToken token = new UsernamePasswordToken(userName, passwd); try { subject.login(token); } catch (UnknownAccountException e) { e.printStackTrace(); model.addAttribute("userName", "用户名错误!"); return "login"; } catch (IncorrectCredentialsException e) { e.printStackTrace(); model.addAttribute("passwd", "密码错误"); return "login"; } return "index"; }
以后的都是HTML页面的跳转
在使用Shiro标签库前,首先须要在JSP引入shiro标签:
<%@ taglib prefix=
"shiro"
uri=
"http://shiro.apache.org/tags"
%>
一、介绍Shiro的标签guest标签 :验证当前用户是否为“访客”,即未认证(包含未记住)的用户。
1
2
3
4
5
|
<shiro:guest>
Hi there! Please <a href=
"login.jsp"
>Login</a> or <a href=
"signup.jsp"
>Signup</a> today!
</shiro:guest>
|
二、user标签 :认证经过或已记住的用户。
1
2
3
4
5
|
<shiro:user>
Welcome back John! Not John? Click <a href=
"login.jsp"
>here<a> to login.
</shiro:user>
|
三、authenticated标签 :已认证经过的用户。不包含已记住的用户,这是与user标签的区别所在。
1
2
3
4
5
|
<shiro:authenticated>
<a href=
"updateAccount.jsp"
>Update your contact information</a>.
</shiro:authenticated>
|
四、notAuthenticated标签 :未认证经过用户,与authenticated标签相对应。与guest标签的区别是,该标签包含已记住用户。
1
2
3
4
5
|
<shiro:notAuthenticated>
Please <a href=
"login.jsp"
>login</a> in order to update your credit card information.
</shiro:notAuthenticated>
|
五、principal 标签 :输出当前用户信息,一般为登陆账号信息。
1
|
Hello, <shiro:principal/>, how are you today?
|
六、hasRole标签 :验证当前用户是否属于该角色。
1
2
3
4
5
|
<shiro:hasRole name=
"administrator"
>
<a href=
"admin.jsp"
>Administer the system</a>
</shiro:hasRole>
|
七、lacksRole标签 :与hasRole标签逻辑相反,当用户不属于该角色时验证经过。
1
2
3
4
5
|
<shiro:lacksRole name=
"administrator"
>
Sorry, you are not allowed to administer the system.
</shiro:lacksRole>
|
八、hasAnyRole标签 :验证当前用户是否属于如下任意一个角色。
1
2
3
4
5
|
<shiro:hasAnyRoles name=
"developer, project manager, administrator"
>
You are either a developer, project manager, or administrator.
</shiro:lacksRole>
|
九、hasPermission标签 :验证当前用户是否拥有指定权限。
1
2
3
4
5
|
<shiro:hasPermission name=
"user:create"
>
<a href=
"createUser.jsp"
>Create a
new
User</a>
</shiro:hasPermission>
|
十、lacksPermission标签 :与hasPermission标签逻辑相反,当前用户没有制定权限时,验证经过。
1
2
3
4
5
|
<shiro:hasPermission name=
"user:create"
>
<a href=
"createUser.jsp"
>Create a
new
User</a>
</shiro:hasPermission>
|
参考文章 :
http://www.cnblogs.com/yangang2013/p/5716928.html
https://www.cnblogs.com/jifeng/p/4500410.html