推荐shiro教程:https://www.w3cschool.cn/shiro/
Spring整合shiro步骤解读:
一:导入shiro相关依赖
二:web.xml中配置shiro过滤器
三:编写自定义的DbRealm,进行认证和授权
四:spring整合shiro配置文件:applicationContext-shiro.xml
五:shiro缓存文件:ehcache-shiro.xml
六:spring容器配置加载applicationContext-shiro.xml文件
一:导入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-ehcache</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.4.0</version>
</dependency>
二:web.xml中配置shiroFilter
<!-- 注意这个过滤器的name,在配置allicationContext-shiro.xml中需要使用 -->
<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>
三:编写自定义的DbRealm,实现用户的认证和授权
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.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import com.mote.pojo.Role;
import com.mote.pojo.User;
import com.mote.service.RoleService;
import com.mote.service.UserService;
public class ShiroDbRealm extends AuthorizingRealm {
@Autowired
private UserService userService;
@Autowired
private RoleService roleService;
/**
* 认证
*/
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken authcToken) throws AuthenticationException {
UsernamePasswordToken token = (UsernamePasswordToken) authcToken;//转为UsernamePasswordToken
String userName = token.getUsername();//获取用户名
String password = new String(token.getPassword());//获取密码
User user = userService.getUserByNamePwd(userName, password);//通過用户名和密码获取用户
if (user == null)
return null;
// 身份认证验证成功,返回一个AuthenticationInfo实现
return new SimpleAuthenticationInfo(user.getUserName(),
password, getName());
}
/**
* 授权
*/
protected AuthorizationInfo doGetAuthorizationInfo(
PrincipalCollection principals) {
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
String userName = (String) getAvailablePrincipal(principals); // 获取用户名
Role role = roleService.getRolesByUserName(userName);// 获取用户角色
if (role == null)
return null;
info.addRole(role.getRoleName()); // 添加角色
List<String> perms = roleService.getPerm(role.getId());// 获取角色对应的权限
info.addStringPermissions(perms); // 添加权限
return info;
}
}
对应的sql语句:
<select id="getUserByNamePwd" resultType="com.mote.pojo.User">
SELECT * FROM user WHERE user_name = #{userName} AND password = #{password}
</select>
<select id="getRolesByUserName" resultType="com.mote.pojo.Role">
SELECT r.id,r.role_name FROM `user` u JOIN role r
ON u.role_id = r.id AND u.user_name = #{userName}
</select>
<select id="getPerm" resultType="String">
SELECT perm_token FROM permission WHERE id IN
(SELECT p.perm_id FROM role_permission p
WHERE p.role_id = #{id})
</select>
四:创建spring整合shiro的配置文件:applicationContext-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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置Realm -->
<bean id="shiroDbRealm" class="com.mote.interceptor.ShiroDbRealm" />
<!-- 配置缓存 -->
<bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:ehcache-shiro.xml" />
</bean>
<!-- 用户退出系统后,清空缓存,解决更新权限后不起作用的问题 -->
<bean id="logoutFilter" class="org.apache.shiro.web.filter.authc.LogoutFilter">
<!-- 跳转登录页面 -->
<property name="redirectUrl" value="/login" />
</bean>
<!-- 配置shiro安全管理器 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="shiroDbRealm" />
<property name="cacheManager" ref="shiroEhcacheManager" />
</bean>
<!-- Shiro Filter -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<!-- 登录页面 ,用户 登录不成功自动 返回该页面 -->
<property name="loginUrl" value="/login" />
<!-- 登录成功页面,登录成功后跳转到该页面 -->
<property name="successUrl" value="/idnex" />
<!-- 无权访问跳转页面 -->
<property name="unauthorizedUrl" value="/noAuthor" />
<property name="filters">
<map>
<!--用户退出系统时,触发logoutFilter -->
<entry key="logout" value-ref="logoutFilter" />
</map>
</property>
<property name="filterChainDefinitions">
<!-- anon任何用户都可以访问,authc,登录才可以访问,user需要shiro进行认证之后才可以访问, -->
<value>
/css/** = anon
/img/** = anon
/js/** = anon
/login = anon
/loginOut = logout <!-- 拦截到/loginOut时,触发shiro退出过滤器 -->
/**/**=user
</value>
</property>
</bean>
<!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
<!-- 支持 Shiro对Controller的方法级AOP安全控制 -->
<bean
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor">
<property name="proxyTargetClass" value="true" />
</bean>
<!-- 开启shiro注解支持 -->
<bean
class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager" />
</bean>
</beans>
关于权限管理filterChainDefinitions过滤器配置可以参考:filterChainDefinitions配置
五:创建ehcache缓存文件:shiro-ehcache.xml
<ehcache updateCheck="false" name="shiroCache">
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
/>
</ehcache>
六:spring容器加载applicationContext-shiro.xml文件
<context-param>
<param-name>contextConfigLocation</param-name >
<param-value>classpath:applicationContext*.xml</param-value >
</context-param>
上一篇:系统的角色管理AND用户管理
下一篇:系统登录逻辑AND授权