|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.dromara.soul.admin.shiro.config; |
| 19 | + |
| 20 | +import org.apache.shiro.realm.AuthorizingRealm; |
| 21 | +import org.apache.shiro.spring.LifecycleBeanPostProcessor; |
| 22 | +import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor; |
| 23 | +import org.apache.shiro.spring.web.ShiroFilterFactoryBean; |
| 24 | +import org.apache.shiro.web.mgt.DefaultWebSecurityManager; |
| 25 | +import org.dromara.soul.admin.shiro.bean.StatelessAuthFilter; |
| 26 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 27 | +import org.springframework.context.annotation.Bean; |
| 28 | +import org.springframework.context.annotation.Configuration; |
| 29 | + |
| 30 | +import javax.servlet.Filter; |
| 31 | +import java.util.LinkedHashMap; |
| 32 | +import java.util.LinkedList; |
| 33 | +import java.util.List; |
| 34 | +import java.util.Map; |
| 35 | + |
| 36 | +/** |
| 37 | + * shiro configuration. |
| 38 | + * |
| 39 | + * @author YuI |
| 40 | + **/ |
| 41 | +@Configuration |
| 42 | +public class ShiroConfiguration { |
| 43 | + |
| 44 | + /** |
| 45 | + * white list. |
| 46 | + */ |
| 47 | + private static final List<String> WHITE_LIST; |
| 48 | + |
| 49 | + static { |
| 50 | + WHITE_LIST = new LinkedList<>(); |
| 51 | + WHITE_LIST.add("/index"); |
| 52 | + WHITE_LIST.add("/platform/login"); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * generate AuthorizingRealm bean. |
| 57 | + * |
| 58 | + * @return {@link AuthorizingRealm} |
| 59 | + */ |
| 60 | + @Bean("shiroRealm") |
| 61 | + public AuthorizingRealm shiroRealm() { |
| 62 | + return new ShiroRealm(); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * generate WebSecurityManager. |
| 67 | + * |
| 68 | + * @param shiroRealm shiro's realm |
| 69 | + * @return {@link DefaultWebSecurityManager} |
| 70 | + */ |
| 71 | + @Bean("shiroSecurityManager") |
| 72 | + public DefaultWebSecurityManager securityManager(@Qualifier("shiroRealm") final AuthorizingRealm shiroRealm) { |
| 73 | + DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); |
| 74 | + securityManager.setRealm(shiroRealm); |
| 75 | + return securityManager; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * ShiroFilterFactoryBean. |
| 80 | + * |
| 81 | + * @param securityManager {@link DefaultWebSecurityManager} |
| 82 | + * @return {@link ShiroFilterFactoryBean} |
| 83 | + */ |
| 84 | + @Bean |
| 85 | + public ShiroFilterFactoryBean shiroFilterFactoryBean( |
| 86 | + @Qualifier("shiroSecurityManager") final DefaultWebSecurityManager securityManager) { |
| 87 | + ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean(); |
| 88 | + factoryBean.setSecurityManager(securityManager); |
| 89 | + Map<String, Filter> filterMap = new LinkedHashMap<>(); |
| 90 | + filterMap.put("statelessAuth", new StatelessAuthFilter()); |
| 91 | + factoryBean.setFilters(filterMap); |
| 92 | + |
| 93 | + Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>(); |
| 94 | + |
| 95 | + for (String s : WHITE_LIST) { |
| 96 | + filterChainDefinitionMap.put(s, "anon"); |
| 97 | + } |
| 98 | + |
| 99 | + filterChainDefinitionMap.put("/**", "statelessAuth"); |
| 100 | + factoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap); |
| 101 | + return factoryBean; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * AuthorizationAttributeSourceAdvisor. |
| 106 | + * |
| 107 | + * @param securityManager {@link DefaultWebSecurityManager} |
| 108 | + * @return {@link AuthorizationAttributeSourceAdvisor} |
| 109 | + */ |
| 110 | + @Bean |
| 111 | + public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor( |
| 112 | + @Qualifier("shiroSecurityManager") final DefaultWebSecurityManager securityManager) { |
| 113 | + AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor(); |
| 114 | + authorizationAttributeSourceAdvisor.setSecurityManager(securityManager); |
| 115 | + return authorizationAttributeSourceAdvisor; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * shiro's lifecycle in spring. |
| 120 | + * |
| 121 | + * @return {@link LifecycleBeanPostProcessor} |
| 122 | + */ |
| 123 | + @Bean |
| 124 | + public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() { |
| 125 | + return new LifecycleBeanPostProcessor(); |
| 126 | + } |
| 127 | + |
| 128 | +} |
0 commit comments