SpringMVC集成Shiro、读取数据库操作权限
1、Maven添加Shiro所需的jar包
<dependency> <groupId>org.apache.shirogroupId> <artifactId>shiro-coreartifactId> <version>${shiroversion}version> dependency> <dependency> <groupId>org.apache.shirogroupId> <artifactId>shiro-ehcacheartifactId> <version>${shiroversion}version> dependency> <dependency> <groupId>org.apache.shirogroupId> <artifactId>shiro-webartifactId> <version>${shiroversion}version> dependency> <dependency> <groupId>org.apache.shirogroupId> <artifactId>shiro-springartifactId> <version>${shiroversion}version> dependency> <dependency> <groupId>com.github.theborakompanionigroupId> <artifactId>thymeleaf-extras-shiroartifactId> <version>2.0.0version> dependency>
ps:老夫用的1.4.0版本, ${shiroversion} 用 1.4.0替代就好
2、添加 spring-shiro.xml文件,解释说明都在注释里了
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"> <bean id="userRealm" class="com.***.shiro.UserRealm"/> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="userRealm"/> bean> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager"/> <property name="loginUrl" value="/login.jsp"/> <property name="successUrl" value="/views/admin/common/master.jsp"/> <property name="filterChainDefinitions"> <value> /static/** = anon /resources/** = anon /admin/login.do = anon /** = authc value> property> bean> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> beans>
3、web.xml里面添加 spring-shiro.xml 的引入
<context-param> <param-name>contextConfigLocationparam-name> <param-value> classpath:spring-base.xml,classpath:spring-mybatis.xml,classpath:spring-druid.xml,classpath:spring-shiro.xml param-value> context-param>
4、在 spring-mvc.xml 里开启 shiro 的的注解
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"> <property name="proxyTargetClass" value="true" /> bean> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> <property name="securityManager" ref="securityManager"/> bean>
配置文件就到此结束了,下面自己创建一个 UserRealm;
5、创建一个继承自AuthorizingRealm的自定义Realm,即指定Shiro验证用户登录的类为自定义的UserRealm.java
package com.***.shiro; import java.util.ArrayList; import java.util.List; import javax.annotation.Resource; import org.apache.commons.lang.builder.ReflectionToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.AuthenticationInfo; import org.apache.shiro.authc.AuthenticationToken; import org.apache.shiro.authc.SimpleAuthenticationInfo; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.authz.AuthorizationException; import org.apache.shiro.authz.AuthorizationInfo; import org.apache.shiro.authz.SimpleAuthorizationInfo; import org.apache.shiro.realm.AuthorizingRealm; import org.apache.shiro.session.Session; import org.apache.shiro.subject.PrincipalCollection; import org.apache.shiro.subject.Subject; import com.**.entity.Admin; import com.**.entity.Permissions; import com.**.entity.Role; import com.**.entity.form.AdminForm; import com.**.entity.form.PermissionsForm; import com.**.entity.form.RoleForm; import com.**.service.IAdminService; import com.**.service.IPermissionsService; import com.**.service.IRoleService; public class UserRealm extends AuthorizingRealm { @Resource private IAdminService adminService; @Resource private IRoleService roleService; @Resource private IPermissionsService permissionsService; /** * 为当前登录的Subject授予角色和权限 */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { // 获取当前登录的用户名,等价于(String)principals.fromRealm(this.getName()).iterator().next() String username = (String) super.getAvailablePrincipal(principals); ListroleList = new ArrayList (); List permissionList = new ArrayList (); // 从数据库中获取当前登录用户的详细信息 AdminForm form = new AdminForm(); form.setUserName(username); Admin admin = adminService.getList(form).get(0); if (null != admin) { // 实体类User中包含有用户角色的实体类信息 if (null != admin.getRoleId()) { // 获取当前登录用户的角色 RoleForm roleForm = new RoleForm(); roleForm.setId(admin.getRoleId()); Role role = roleService.getList(roleForm).get(0); roleList.add(role.getName()); // 实体类Role中包含有角色权限的实体类信息 if (null != role.getPermissionsList()) { String permissionsList[] = role.getPermissionsList().split(","); // 获取权限 for (int i = 0; i < permissionsList.length; i++) { PermissionsForm permissionsForm = new PermissionsForm(); permissionsForm.setId(Integer.parseInt(permissionsList[i])); Permissions permi = permissionsService.getList(permissionsForm).get(0); permissionList.add(permi.getCode()); } } } } else { throw new AuthorizationException(); } // 为当前用户设置角色和权限 SimpleAuthorizationInfo simpleAuthorInfo = new SimpleAuthorizationInfo(); simpleAuthorInfo.addRoles(roleList); simpleAuthorInfo.addStringPermissions(permissionList); return simpleAuthorInfo; } /** * 验证当前登录的Subject * * @see 经测试:本例中该方法的调用时机为LoginController.login()方法中执行Subject.login()时 */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException { // 获取基于用户名和密码的令牌 // 实际上这个authcToken是从AdminController里面currentUser.login(token)传过来的 UsernamePasswordToken token = (UsernamePasswordToken) authcToken; System.err.println( "验证当前Subject时获取到token为" + ReflectionToStringBuilder.toString(token, ToStringStyle.MULTI_LINE_STYLE)); AdminForm form = new AdminForm(); form.setUserName(token.getUsername()); Admin admin = adminService.getList(form).get(0); if (null != admin) { AuthenticationInfo authcInfo = new SimpleAuthenticationInfo(admin.getUserName(), admin.getPassword(), admin.getId().toString()); this.setSession("currentUser", admin); return authcInfo; } else { return null; } } /** * 将一些数据放到ShiroSession中,以便于其它地方使用 * * @see 比如Controller,使用时直接用HttpSession.getAttribute(key)就可以取到 */ private void setSession(Object key, Object value) { Subject currentUser = SecurityUtils.getSubject(); if (null != currentUser) { Session session = currentUser.getSession(); System.out.println("Session默认超时时间为[" + session.getTimeout() + "]毫秒"); if (null != session) { session.setAttribute(key, value); } } } }
数据库、实体、Dao配置省略。。。
附:表片段
6、在登录方法中,登录成功后将用户添加到 shiro 的 Subject 中
UsernamePasswordToken token = new UsernamePasswordToken(admin.getUserName(), admin.getPassword()); Subject currentUser = SecurityUtils.getSubject(); currentUser.login(token);
7、在需要访问权限的方法上添加 @RequiresPermissions() 注解
@RequestMapping("/permissionsList") @RequiresPermissions("permissionsList") public ModelAndView permissionsList(HttpServletRequest request) { ModelAndView mv = new ModelAndView(); PermissionsForm form = new PermissionsForm(); if (!StringUtils.isEmpty(request.getParameter("id"))) form.setId(Integer.parseInt(request.getParameter("id"))); if (!StringUtils.isEmpty(request.getParameter("name"))) form.setName(request.getParameter("name")); if (!StringUtils.isEmpty(request.getParameter("group"))) form.setGroup(request.getParameter("group")); ListmenuList = permissionsService.getList(form); mv.addObject("menuList", menuList); mv.addObject("id", request.getParameter("id")); mv.addObject("name", request.getParameter("name")); mv.addObject("group", request.getParameter("group")); mv.setViewName("views/admin/system_manage/permissions_list"); return mv; }
@RequiresPermissions("permissionsList") : 表示拥有 permissionsList 权限方可访问该方法
附加:
实际使用访问到不具有权限的地址时会报错 org.apache.shiro.authz.UnauthorizedException 或者 org.apache.shiro.authz.UnauthenticatedException
此时需要在 spring-mvc.xml 里添加 错误跳转未授权页面
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="org.apache.shiro.authz.UnauthorizedException">no_permissionsprop> <prop key="org.apache.shiro.authz.UnauthenticatedException">no_permissionsprop> props> property> bean>
no_permissions 为 ModelAndView。
完结。。。