学习笔记 : SSM整合Shiro的基本配置
学习Spring Boot整合Shiro知识后,写了一个简单的Spring Boot整合Shiro的权限管理案例,可随后在GitHub上发现了一个基于SSM框架简单的后台权限管理系统,很喜欢该项目(比较初级,适合练手),所以又开始学习如何在SSM中整合Shiro,Spring集成Shiro一般通过的 xml 配置,相比Spring Boot较为繁琐哟~ 以下基本配置信息摘自案例 : 一个简单的SSM整合Shiro的权限管理案例
Maven依赖
Shiro-learning/ssm project with shiro/ssm-shiro/pom.xml
1 2 3 4 5 6 7 8 9 10 11 12
| <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.4.1</version> </dependency>
<dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-ehcache</artifactId> <version>1.4.1</version> </dependency>
|
web.xml配置
Shiro-learning/ssm project with shiro/ssm-shiro/src/main/webapp/WEB-INF/web.xml
1 2 3 4 5 6 7 8 9 10 11 12
|
<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>
|
Shiro的基本配置
Shiro-learning/ssm project with shiro/ssm-shiro/src/main/resources/spring-config/applicationContext-shiro.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| <?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">
<description>configuration informations about Shiro</description>
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager"/> <property name="filterChainDefinitions"> <value> /login=anon /logout=logout /teaListView=roles[admin] /stuListView=perms[stuListView:view] /teaListView=perms[teaListView:view] /**=authc </value> </property> <property name="loginUrl" value="/loginView"/> <property name="unauthorizedUrl" value="/unauthorizedView"/> </bean>
<bean id="realm" class="pers.huangyuhui.shiro.shiro.UserRealm"> <property name="credentialsMatcher" ref="credentialsMatcher"/> <property name="cachingEnabled" value="true"/> <property name="authenticationCachingEnabled" value="true"/> <property name="authenticationCacheName" value="authenticationCache"/> <property name="authorizationCachingEnabled" value="true"/> <property name="authorizationCacheName" value="authorizationCache"/> </bean>
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="realm"/> <property name="cacheManager" ref="ehCacheManager"/> </bean>
<bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher"> <property name="hashAlgorithmName" value="MD5"/> <property name="hashIterations" value="3"/> </bean>
<bean id="ehCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> <property name="cacheManagerConfigFile" value="classpath:shiro-config/ehcache-shiro.xml"/> </bean>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<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>
</beans>
|
Shiro-learning/ssm project with shiro/ssm-shiro/src/main/resources/shiro-config/ehcache-shiro.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| <?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false" name="myEhcache"> <diskStore path="java.io.tmpdir"/> <cache name="authorizationCache" maxEntriesLocalHeap="2000" timeToIdleSeconds="1800" timeToLiveSeconds="1800" overflowToDisk="false" statistics="true"> </cache> <cache name="authenticationCache" maxEntriesLocalHeap="2000" timeToIdleSeconds="1800" timeToLiveSeconds="1800" overflowToDisk="false" statistics="true"> </cache> <cache name="activeSessionCache" maxEntriesLocalHeap="2000" timeToIdleSeconds="1800" timeToLiveSeconds="1800" overflowToDisk="false" statistics="true"> </cache> <cache name="halfHour" maxElementsInMemory="10000" maxElementsOnDisk="100000" timeToIdleSeconds="1800" timeToLiveSeconds="1800" overflowToDisk="false" diskPersistent="false"/> <cache name="hour" maxElementsInMemory="10000" maxElementsOnDisk="100000" timeToIdleSeconds="3600" timeToLiveSeconds="3600" overflowToDisk="false" diskPersistent="false"/> <cache name="oneDay" maxElementsInMemory="10000" maxElementsOnDisk="100000" timeToIdleSeconds="86400" timeToLiveSeconds="86400" overflowToDisk="false" diskPersistent="false"/>
<defaultCache name="defaultCache" maxElementsInMemory="10000" timeToIdleSeconds="600" timeToLiveSeconds="600" overflowToDisk="false" maxElementsOnDisk="100000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> </defaultCache> </ehcache>
|
自定义Realm的基本配置
Shiro-learning/ssm project with shiro/ssm-shiro/src/main/java/pers/huangyuhui/shiro/shiro/UserRealm.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| package pers.huangyuhui.shiro.shiro;
import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.*; 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.apache.shiro.subject.Subject; import org.apache.shiro.util.ByteSource; import org.springframework.beans.factory.annotation.Autowired; import pers.huangyuhui.shiro.bean.Permission; import pers.huangyuhui.shiro.bean.Role; import pers.huangyuhui.shiro.bean.User; import pers.huangyuhui.shiro.service.UserService;
import java.util.Collection; import java.util.HashSet; import java.util.Set;
public class UserRealm extends AuthorizingRealm {
@Autowired private UserService userService;
@Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { System.err.println("------------> 授权认证 ------------>"); Subject currentUser = SecurityUtils.getSubject(); User u = (User) currentUser.getPrincipal(); User user = userService.findByName(u.getUsername()); if (user != null) { SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); Collection<String> rolesCollection = new HashSet<>(); Collection<String> permissionsCollection = new HashSet<>(); Set<Role> roles = user.getRoles(); for (Role role : roles) { rolesCollection.add(role.getName()); Set<Permission> permissionSet = role.getPermissions(); for (Permission permission : permissionSet) { permissionsCollection.add(permission.getName()); } info.addStringPermissions(permissionsCollection); } info.addRoles(rolesCollection); System.out.println("[roles]------------>" + rolesCollection.toString()); System.out.println("[permissions]------------>" + permissionsCollection.toString()); return info; } return null;
}
@Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { System.err.println("------------> 开始认证 ------------>"); UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken; User user = userService.findByName(token.getUsername()); System.out.println("[user]------------>" + user); if (user == null) { return null; } return new SimpleAuthenticationInfo(user, user.getPassword(), ByteSource.Util.bytes(user.getUsername()), this.getName()); } }
|