springMVC中快速配置shiro

来源:互联网 发布:网络购物诈骗定义 编辑:程序博客网 时间:2024/06/05 16:58

1.在web.xml文件中配置shiro的过滤器,以拦截项目内的访问

<!-- shiro过滤器定义 --><filter>  <filter-name>shiroFilter</filter-name>  <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  <init-param>  <!-- 该值缺省为false,表示生命周期由SpringApplicationContext管理,设置为true则表示由ServletContainer管理 -->  <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>

2.在spring 的配置文件中配置shiro的相关项

<!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->  <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>    <!-- 缓存管理 -->  <bean id="cacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager"></bean>  <!-- 自定义Realm --><bean id="myRealm" class="com.bf.planner.realm.MyRealm"/>  <!-- 自定义reaml获取令牌以及角色和权限的类 --><!-- 安全管理器 --><bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">  <property name="realm" ref="myRealm"/>  </bean>  <!-- Shiro过滤器 --><bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">  <!-- Shiro的核心安全接口,这个属性是必须的 -->  <property name="securityManager" ref="securityManager"/><!-- 身份认证失败,则跳转到登录页面的配置 -->  <property name="loginUrl" value="/"/><!-- 权限认证失败,则跳转到指定页面 -->  <property name="unauthorizedUrl" value="/unauthor.jsp"/>  <!-- Shiro连接约束配置,即过滤链的定义 -->  <property name="filterChainDefinitions">  <value>/** = authc<!-- 配置访问项目下的所有路径均需要验证令牌(即登录) --></value>  </property></bean>  


3.在对应包下定义Reaml(我在spring中配置的是com.bf.planner.realm.MyRealm,所以我在com.bf.planner.realm包下新建了一个MyRealm类)

public class MyRealm extends AuthorizingRealm{@Autowiredprivate LoginService loginService;/** * 用户授权 */@Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {String userName=(String)principals.getPrimaryPrincipal();//获取当前登录用户的用户名SimpleAuthorizationInfo authorizationInfo=new SimpleAuthorizationInfo();//新建权限的对象authorizationInfo.setRoles(loginService.getRoles(userName));//设置此用户对应的角色authorizationInfo.setStringPermissions(loginService.getPermissions(userName));//设置此用户对应的权限return authorizationInfo;}/** * 用户登录 */@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {String user_name=(String)token.getPrincipal();//获取登录者的用户名User user=loginService.getUser(user_name);//根据用户名查出数据库中所对应的用户信息if(user!=null){AuthenticationInfo authcInfo=new SimpleAuthenticationInfo(user.getUser_name(),user.getPassword(),"xx");//设置此用户的令牌,即设置正确的用户名与密码return authcInfo;}else{return null;}}}


4.登录时使用shiro来验证

@RequestMapping("/login")@ResponseBodypublic ResultInfo login(User user){ResultInfo resultInfo=new ResultInfo();Subject subject=SecurityUtils.getSubject();UsernamePasswordToken token=new UsernamePasswordToken(user.getUser_name(), user.getPassword());try{subject.login(token);resultInfo.setCode(true); //登录成功return resultInfo;}catch(Exception e){resultInfo.setCode(false);//登录失败,如果没有对应的用户,则会抛出异常return resultInfo;}}

5.使用shiro来控制方法的访问权限(例子):

@RequiresRoles(value={"admin","base"}, logical = Logical.OR)@RequiresPermissions(value = {"add","update"}, logical = Logical.AND)public void shiro_test(){}

上面的代码中对shiro_test方法添加了访问限制:

@RequiresRoles控制拥有admin或base角色的用户可以访问此方法

@RequiresPermissions控制拥有add和update权限的用户可以访问此方法



6.页面中使用shiro来控制页面内容的显示:

首先,需要在页面头部引入shiro支持:

<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
然后,我们可以在页面中使用shiro的标签了:(<shiro:principal/>当前登录用户的用户名【即登录账号】)

<shiro:hasRole name="admin">欢迎有admin角色的用户!<shiro:principal/> <!-- 登录人 --></shiro:hasRole><shiro:hasPermission name="student:create">欢迎有student:create权限的用户!<shiro:principal/></shiro:hasPermission>


注意:从数据库中获取的角色集合和权限集合中不允许有空值



0 0
原创粉丝点击