管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2022-09-28 f88e25e5a60297d5db0adb6464732781b3bf9370
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package com.lf.server.config;
 
import com.alibaba.druid.pool.DruidDataSource;
import com.lf.server.entity.sys.MyRealm;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.codec.Base64;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * ShiroConfig
 * @author WWW
 */
@SuppressWarnings("ALL")
//@Configuration
public class ShiroConfig {
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DruidDataSource druidDataSource() {
        return new DruidDataSource();
    }
 
    /*@Bean(name = "securityManager")
    public DefaultWebSecurityManager securityManager(@Qualifier("myRealm") MyRealm myRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(myRealm);
 
        return securityManager;
    }*/
 
    @Bean(name = "securityManager")
    public DefaultWebSecurityManager securityManager(@Qualifier("myRealm") MyRealm myRealm,
                                                     @Qualifier("rememberMeManager") CookieRememberMeManager rememberMeManager,
                                                     @Qualifier("mySessionManager") DefaultWebSessionManager webSessionManager) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(myRealm);
        securityManager.setRememberMeManager(rememberMeManager);
        securityManager.setSessionManager(webSessionManager);
 
        return securityManager;
    }
 
    @Bean
    public MyRealm myRealm() {
        MyRealm myShiroRealm = new MyRealm();
 
        return myShiroRealm;
    }
 
    @Bean(name = "rememberMeManager")
    public CookieRememberMeManager rememberMeManager() {
        // cookie管理器
        CookieRememberMeManager cookieRememberMeManager = new CookieRememberMeManager();
 
        // cookie的名字
        SimpleCookie simpleCookie = new SimpleCookie("rememberMe");
 
        // 设置有效期时间30天,秒
        simpleCookie.setMaxAge(259200);
 
        cookieRememberMeManager.setCookie(simpleCookie);
 
        // rememberMe cookie加密的密钥 建议每个项目都不一样 默认AES算法 密钥长度(128 256 512 位)
        cookieRememberMeManager.setCipherKey(Base64.decode("6ZmI6I2j5Y+R5aSn5ZOlAA=="));
 
        return cookieRememberMeManager;
    }
 
    /**
     * 创建DefaultWebSessionManager类
     *
     * @return
     */
    @Bean(name = "mySessionManager")
    public DefaultWebSessionManager mySessionManager() {
        DefaultWebSessionManager defaultSessionManager = new DefaultWebSessionManager();
 
        // 将sessionIdUrlRewritingEnabled属性设置成false
        defaultSessionManager.setSessionIdUrlRewritingEnabled(false);
 
        defaultSessionManager.setGlobalSessionTimeout(8 * 60 * 60 * 1000);
        defaultSessionManager.setSessionValidationSchedulerEnabled(true);
        defaultSessionManager.setSessionIdCookieEnabled(true);
 
        return defaultSessionManager;
    }
 
    @Bean
    public ShiroFilterFactoryBean bean(@Qualifier("securityManager") DefaultWebSecurityManager securityManager) {
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        bean.setSecurityManager(securityManager);
 
        /**
         * 添加shiro的内置过滤器
         *  anon: 无需认证即可访问
         *  authc: 必须认证才能用
         *  user: 必须拥有 “记住我” 功能才能用
         *  perms: 拥有对某个资源的权限才能用
         *  role: 拥有某个角色权限才能访问
         */
        Map<String, String> filterMap = new HashMap<>(5);
 
        // 登陆后授权,正常情况下没有授权会跳转到未授权页面
        //filterMap.put("/toAdd", "perms[user:add]");
        //filterMap.put("/toUpdate", "perms[user:update]");
 
        // 登出,设置注销过滤器
        //filterMap.put("/logout", "logout");
 
        /**
         *    /** 匹配所有的路径
         *   通过Map集合组成了一个拦截器链 ,自顶向下过滤,一旦匹配,则不再执行下面的过滤
         *   如果下面的定义与上面冲突,那按照了谁先定义谁说了算
         *   所以/** 一定要配置在最后
         *   这里是否要对所有路径进行认证视情况而定,因为一些路由跳转可能在没登陆出现导致出错,所以这里考虑清楚
         **/
        //filterMap.put("/**", "authc");
 
        // 将拦截器链设置到shiro中
        bean.setFilterChainDefinitionMap(filterMap);
 
        // 设置登录页面
        bean.setLoginUrl("/toLogin");
 
        // 登录成功后要跳转的链接
        //bean.setSuccessUrl("/toIndex");
 
        // 设置未授权页面
        bean.setUnauthorizedUrl("/noauth");
 
        return bean;
    }
 
    /**
     * 密码匹配凭证管理器
     *
     * 密码校验规则HashedCredentialsMatcher
     * 这个类是为了对密码进行编码的 ,
     * 防止密码在数据库里明码保存 , 当然在登陆认证的时候 ,
     * 这个类也负责对form里输入的密码进行编码
     * 处理认证匹配处理器:如果自定义需要实现继承HashedCredentialsMatcher
     */
    @Bean(name = "hashedCredentialsMatcher")
    public HashedCredentialsMatcher hashedCredentialsMatcher() {
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
 
        // 散列算法:这里使用MD5算法;
        hashedCredentialsMatcher.setHashAlgorithmName("MD5");
 
        // 散列的次数,比如散列两次,相当于md5(md5(""));
        hashedCredentialsMatcher.setHashIterations(1024);
        hashedCredentialsMatcher.setStoredCredentialsHexEncoded(true);
 
        return hashedCredentialsMatcher;
    }
 
    /**
     * 开启shiro aop注解支持
     * 使用代理方式;所以需要开启代码支持
     * @param securityManager
     */
    @Bean
    public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator(){
        DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        advisorAutoProxyCreator.setProxyTargetClass(true);
 
        return advisorAutoProxyCreator;
    }
 
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultWebSecurityManager securityManager){
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
 
        return authorizationAttributeSourceAdvisor;
    }
 
    /**
     * 开启cglib代理
     */
    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator creator = new DefaultAdvisorAutoProxyCreator();
        creator.setProxyTargetClass(true);
 
        return creator;
    }
}