1. web .Xml configuration
// turn on the filter <filter> <filter-name>shiroFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> // filter all <filter-mapping> <filter-name>shiroFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
2. Spring.Xml configuration
<!-- Configure the filter factory object of the shiro framework --> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <!-- inject security manager object --> <property name="securityManager" ref="securityManager"/> <!-- Inject related page access URL --> <property name="loginUrl" value="login.html"/> <property name="successUrl" value="success.html"/> <property name="unauthorizedUrl" value="403.html"/> <!--Inject URL blocking rules--> <property name="filterChainDefinitions"> <value> /css/** = anon /js/** = anon /images/** = anon /validatecode.jsp* = anon /login.html = anon /user/login.html = anon /page/base/staff* = perms["staffList"] /* = authc </value> </property> </bean> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="bosRealm"/> </bean> <!-- register realm --> <bean id="bosRealm" class="com.springshirodemo.Realm.CustomRealm"> <property name="credentialsMatcher" ref="credentialsMatcher"/> </bean> <!--Set encryption algorithm--> <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher" id="credentialsMatcher"> <property name="hashAlgorithmName" value="md5"/> <property name="hashIterations" value="2"/> </bean> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> <!-- Use annotations --> <context:annotation-config></context:annotation-config> </beans>
3.SpringMVC.xml placement
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:component-scan base-package="com.springshirodemo.controller"/> <mvc:annotation-driven/> <!--Exclude static files--> <mvc:resources mapping="/*" location="/"/> <aop:config proxy-target-class="true"/> <bean class ="org.apache.shiro.spring.LifecycleBeanPostProcessor"></bean> <bean class ="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"></bean> </beans>
4. Customize CustomRealm
package com.springshirodemo.Realm; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; 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.authz.AuthorizationInfo; import org.apache.shiro.authz.SimpleAuthorizationInfo; import org.apache.shiro.crypto.hash.Md5Hash; import org.apache.shiro.realm.AuthorizingRealm; import org.apache.shiro.subject.PrincipalCollection; import org.apache.shiro.util.ByteSource; public class CustomRealm extends AuthorizingRealm{ Map<String, String> usesMap = new HashMap<String ,String>(); { usesMap.put("mack", "3285541c519ec7cef7077b06baae58d5"); super.setName("CustomRealm"); } @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { // TODO Auto-generated method stub String role = (String) principals.getPrimaryPrincipal(); Set <String> roles = getRolesByUserName (); Set<String> Permissions = getPermissionsByUserName(); SimpleAuthorizationInfo AuthorizationInfo = new SimpleAuthorizationInfo(); AuthorizationInfo.setRoles(roles); AuthorizationInfo.setStringPermissions(Permissions); return AuthorizationInfo; } private Set<String> getPermissionsByUserName() { // TODO Auto-generated method stub Set<String> Permissions = new HashSet<String>(); Permissions.add("user:delect"); Permissions.add("user:update"); Permissions.add("user:insert"); return Permissions; } private Set <String> getRolesByUserName () { // TODO Auto-generated method stub Set<String> roles = new HashSet<String>(); roles.add("admin"); roles.add("user"); return roles; } @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { // TODO Auto-generated method stub // èŽ · å¾ — ä¸ »ä½“ 认è¯ä¿¡æ¯èŽ · å¾ — ç ”¨æˆ · å? String username = (String) token.getPrincipal(); / * ä »Žæ • ° æ®åº“ ä¸æŸ ¥ è¯? * / String password = getDateuser(username); if(password == null) { return null; } SimpleAuthenticationInfo authenticationinfo = new SimpleAuthenticationInfo("CustomRealm",password,"CustomRealm"); authenticationinfo.setCredentialsSalt (ByteSource.Util.bytes ("mark")); return authenticationinfo; } private String getDateuser(String username) { // TODO Auto-generated method stub return usesMap.get(username); } public static void main(String[] args) { Md5Hash md5 = new Md5Hash("123456","mark",2); System.out.println(md5); } }