zrj
2024-06-05 6b41922408dea0e31607916c20c0026cc3c501a5
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
package org.springblade.auth.config;
 
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springblade.auth.constant.Oauth2Constants;
import org.springblade.auth.handle.TokenFilterHandle;
import org.springblade.auth.support.BladePasswordEncoderFactories;
import org.springblade.core.secure.registry.SecureRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
 
import javax.annotation.Resource;
 
/**
 * Security配置
 *
 * @author Chill
 */
@Configuration(proxyBeanMethods = false)
@AllArgsConstructor
@Order(1)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
 
    @Resource
    private Oauth2Constants oauth2Constants;
 
    @Autowired
    private TokenFilterHandle tokenFilterHandle;
 
    /**
     * 安全框架配置
     */
    @Bean
    public SecureRegistry secureRegistry() {
        SecureRegistry secureRegistry = new SecureRegistry();
//        secureRegistry.setEnabled(false);
        secureRegistry.setEnabled(true);
        secureRegistry.excludePathPatterns("/oauth/login");
        secureRegistry.excludePathPatterns("/oauth/authorize");
        secureRegistry.excludePathPatterns("/oauth/form");
        secureRegistry.excludePathPatterns("/blade-system/menu/routes");
        secureRegistry.excludePathPatterns("/blade-system/menu/auth-routes");
        secureRegistry.excludePathPatterns("/blade-system/menu/top-menu");
        secureRegistry.excludePathPatterns("/blade-system/tenant/info");
        secureRegistry.excludePathPatterns("/blade-flow/process/resource-view");
        secureRegistry.excludePathPatterns("/blade-flow/process/diagram-view");
        secureRegistry.excludePathPatterns("/blade-flow/manager/check-upload");
        secureRegistry.excludePathPatterns("/doc.html");
        secureRegistry.excludePathPatterns("/js/**");
        secureRegistry.excludePathPatterns("/webjars/**");
        secureRegistry.excludePathPatterns("/swagger-resources/**");
        secureRegistry.excludePathPatterns("/druid/**");
        return secureRegistry;
    }
 
    @Bean
    @Override
    @SneakyThrows
    public AuthenticationManager authenticationManagerBean() {
        return super.authenticationManagerBean();
    }
 
    @Bean
    public PasswordEncoder passwordEncoder() {
        return BladePasswordEncoderFactories.createDelegatingPasswordEncoder();
    }
 
    @Override
    @SneakyThrows
    protected void configure(HttpSecurity http) {
        http.headers().frameOptions().disable();
        http.csrf().disable();
        http.formLogin()
            //自定义认证成功跳转
            .successHandler(new CustomAuthenticationSuccessHandler(oauth2Constants.getAuthorizeUrl()))
            // 自定义登录页面
            .loginPage(oauth2Constants.getLoginPage())
            // 自定义登录接口url
            .loginProcessingUrl(oauth2Constants.getLoginProcessingUrl())
            // 自定义登录失败处理
            .failureHandler(new CustomAuthenticationFailureHandler())
        ;
        // 认证失败自定义登录页跳转
        http.exceptionHandling()
            .authenticationEntryPoint(new CustomAuthenticationEntryPoint(oauth2Constants.getLoginPage()));
 
        //token 校验在前
        http.addFilterBefore(tokenFilterHandle, UsernamePasswordAuthenticationFilter.class);
 
    }
 
    @Override
    public void configure(WebSecurity web) {
        web.ignoring().antMatchers("/templates/**","/js/*.js", "/css/*.css");
    }
 
}