¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.yuanchu.limslaboratory.shiro.config; |
| | | |
| | | import com.yuanchu.limslaboratory.shiro.filter.JwtFilter; |
| | | import com.yuanchu.limslaboratory.shiro.MultiRealmAuthenticator; |
| | | import com.yuanchu.limslaboratory.shiro.realm.JwtRealm; |
| | | import com.yuanchu.limslaboratory.shiro.realm.ShiroRealm; |
| | | import com.yuanchu.limslaboratory.shiro.utils.JwtCredentialsMatcher; |
| | | import org.apache.shiro.authc.credential.CredentialsMatcher; |
| | | import org.apache.shiro.authc.credential.HashedCredentialsMatcher; |
| | | import org.apache.shiro.authc.pam.AuthenticationStrategy; |
| | | import org.apache.shiro.authc.pam.FirstSuccessfulStrategy; |
| | | import org.apache.shiro.authc.pam.ModularRealmAuthenticator; |
| | | import org.apache.shiro.authz.Authorizer; |
| | | import org.apache.shiro.authz.ModularRealmAuthorizer; |
| | | import org.apache.shiro.crypto.hash.Md5Hash; |
| | | import org.apache.shiro.mgt.DefaultSessionStorageEvaluator; |
| | | import org.apache.shiro.mgt.DefaultSubjectDAO; |
| | | import org.apache.shiro.mgt.SecurityManager; |
| | | import org.apache.shiro.mgt.SessionStorageEvaluator; |
| | | import org.apache.shiro.realm.Realm; |
| | | import org.apache.shiro.spring.LifecycleBeanPostProcessor; |
| | | import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor; |
| | | import org.apache.shiro.spring.web.ShiroFilterFactoryBean; |
| | | import org.apache.shiro.web.mgt.DefaultWebSecurityManager; |
| | | import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator; |
| | | import org.springframework.boot.web.servlet.FilterRegistrationBean; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | |
| | | import javax.servlet.Filter; |
| | | import java.util.*; |
| | | |
| | | @Configuration |
| | | public class ShiroConfig { |
| | | |
| | | /** |
| | | * ä¸å Spring容å¨ä¸æ³¨å JwtFilter Beanï¼é²æ¢ Spring å° JwtFilter 注å为å
¨å±è¿æ»¤å¨ |
| | | * å
¨å±è¿æ»¤å¨ä¼å¯¹ææè¯·æ±è¿è¡æ¦æªï¼èæ¬ä¾ä¸åªéè¦æ¦æªé¤ /login å /logout å¤çè¯·æ± |
| | | * å¦ä¸ç§ç®ååæ³æ¯ï¼ç´æ¥å»æ jwtFilter()ä¸ç @Bean 注解 |
| | | */ |
| | | @Bean |
| | | public FilterRegistrationBean<Filter> registration(JwtFilter filter) { |
| | | FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<Filter>(filter); |
| | | registration.setEnabled(false); |
| | | return registration; |
| | | } |
| | | |
| | | //ShiroFilterè¿æ»¤ææè¯·æ± |
| | | @Bean |
| | | public ShiroFilterFactoryBean getShiroFilterFactoryBean(SecurityManager securityManager) { |
| | | ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); |
| | | //ç»ShiroFilteré
ç½®å®å
¨ç®¡çå¨ |
| | | shiroFilterFactoryBean.setSecurityManager(securityManager); |
| | | |
| | | // æ·»å jwt ä¸ç¨è¿æ»¤å¨ï¼æ¦æªé¤ /login å /logout å¤çè¯·æ± |
| | | Map<String, Filter> filterMap = new LinkedHashMap<>(); |
| | | filterMap.put("jwtFilter", new JwtFilter()); |
| | | shiroFilterFactoryBean.setFilters(filterMap); |
| | | |
| | | //é
置系ç»å
Œ
±èµæº |
| | | Map<String, String> map = new HashMap<String, String>(); |
| | | |
| | | // swaggeræ¾è¡ |
| | | map.put("/doc.html", "anon"); |
| | | map.put("/webjars/**/**", "anon"); |
| | | map.put("/swagger-resources", "anon"); |
| | | map.put("/api-docs", "anon"); |
| | | map.put("/v3/**", "anon"); |
| | | |
| | | map.put("/link-basic/*", "anon"); |
| | | map.put("/user/login/**","anon");//表示è¿ä¸ªä¸ºå
Œ
±èµæº ä¸å®æ¯å¨åéèµæºä¸é¢ |
| | | // map.put("/**","jwtFilter");//表示è¿ä¸ªèµæºéè¦è®¤è¯åææ |
| | | |
| | | shiroFilterFactoryBean.setFilterChainDefinitionMap(map); |
| | | |
| | | return shiroFilterFactoryBean; |
| | | } |
| | | |
| | | /** |
| | | * é
ç½® ModularRealmAuthenticator |
| | | */ |
| | | @Bean |
| | | public ModularRealmAuthenticator authenticator() { |
| | | ModularRealmAuthenticator authenticator = new MultiRealmAuthenticator(); |
| | | // è®¾ç½®å¤ Realmç认è¯çç¥ï¼é»è®¤ AtLeastOneSuccessfulStrategy |
| | | AuthenticationStrategy strategy = new FirstSuccessfulStrategy(); |
| | | authenticator.setAuthenticationStrategy(strategy); |
| | | return authenticator; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * ç¦ç¨session, ä¸ä¿åç¨æ·ç»å½ç¶æãä¿è¯æ¯æ¬¡è¯·æ±é½éæ°è®¤è¯ |
| | | */ |
| | | @Bean |
| | | protected SessionStorageEvaluator sessionStorageEvaluator() { |
| | | DefaultSessionStorageEvaluator sessionStorageEvaluator = new DefaultSessionStorageEvaluator(); |
| | | sessionStorageEvaluator.setSessionStorageEnabled(false); |
| | | return sessionStorageEvaluator; |
| | | } |
| | | |
| | | /** |
| | | * é
ç½® SecurityManagerï¼æé管çå¨ |
| | | */ |
| | | @Bean |
| | | public DefaultWebSecurityManager securityManager() { |
| | | DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); |
| | | |
| | | // 1.身份éªè¯å¨ |
| | | securityManager.setAuthenticator(authenticator()); |
| | | |
| | | // 2.管çRealm |
| | | List<Realm> realms = new ArrayList<Realm>(16); |
| | | realms.add(JwtRealm()); |
| | | realms.add(shiroRealm()); |
| | | securityManager.setRealms(realms); // é
ç½®å¤ä¸ªrealm |
| | | |
| | | // 3.å
³éshiroèªå¸¦çsession |
| | | DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO(); |
| | | subjectDAO.setSessionStorageEvaluator(sessionStorageEvaluator()); |
| | | securityManager.setSubjectDAO(subjectDAO); |
| | | |
| | | return securityManager; |
| | | } |
| | | |
| | | // å建èªå®ä¹Realm |
| | | @Bean |
| | | public Realm shiroRealm() { |
| | | ShiroRealm realm = new ShiroRealm(); |
| | | // HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher(); |
| | | //// //设置使ç¨MD5å å¯ç®æ³ |
| | | //// credentialsMatcher.setHashAlgorithmName(Md5Hash.ALGORITHM_NAME); |
| | | //// //æ£åæ¬¡æ° |
| | | //// credentialsMatcher.setHashIterations(1024); |
| | | // realm.setCredentialsMatcher(credentialsMatcher); |
| | | return realm; |
| | | } |
| | | |
| | | @Bean |
| | | public Realm JwtRealm(){ |
| | | JwtRealm jwtRealm = new JwtRealm(); |
| | | // 设置å å¯ç®æ³ |
| | | CredentialsMatcher credentialsMatcher = new JwtCredentialsMatcher(); |
| | | // 设置å 坿¬¡æ° |
| | | jwtRealm.setCredentialsMatcher(credentialsMatcher); |
| | | return jwtRealm; |
| | | } |
| | | |
| | | |
| | | // 以ä¸ä¸ä¸beanéç¨ï¼åºå®é
ç½® |
| | | |
| | | /** |
| | | * äº¤ç± Spring æ¥èªå¨å°ç®¡ç Shiro-Bean ççå½å¨æ |
| | | */ |
| | | @Bean |
| | | public static LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() { |
| | | return new LifecycleBeanPostProcessor(); |
| | | } |
| | | |
| | | /** |
| | | * 为 Spring-Bean å¼å¯å¯¹ Shiro æ³¨è§£çæ¯æ |
| | | */ |
| | | @Bean |
| | | public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) { |
| | | AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor(); |
| | | authorizationAttributeSourceAdvisor.setSecurityManager(securityManager); |
| | | return authorizationAttributeSourceAdvisor; |
| | | } |
| | | |
| | | /** |
| | | * å¼å¯AOPæ¹æ³çº§æéæ£æ¥ |
| | | */ |
| | | @Bean |
| | | public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() { |
| | | DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator(); |
| | | advisorAutoProxyCreator.setProxyTargetClass(true); |
| | | return advisorAutoProxyCreator; |
| | | } |
| | | } |