田源
2024-03-01 02b3d584d201ca7cb8a024fd151fe6eddbf43def
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
/*
 *      Copyright (c) 2018-2028, DreamLu All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: DreamLu 卢春梦 (596392912@qq.com)
 */
package com.vci.ubcs.admin.config;
 
import com.vci.ubcs.admin.security.CustomAuthenticationManager;
import de.codecentric.boot.admin.server.config.AdminServerProperties;
import com.vci.ubcs.admin.security.InternalAuthorizationManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.ReactiveAuthenticationManager;
import org.springframework.security.authorization.ReactiveAuthorizationManager;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.server.SecurityWebFilterChain;
import org.springframework.security.web.server.authentication.RedirectServerAuthenticationSuccessHandler;
import org.springframework.security.web.server.authorization.AuthorizationContext;
 
import java.net.URI;
 
/**
 * 监控安全配置
 *
 * @author L.cm
 */
@EnableWebFluxSecurity
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(AdminServerProperties.class)
public class SecurityConfiguration {
    private final String contextPath;
 
    public SecurityConfiguration(AdminServerProperties adminServerProperties) {
        this.contextPath = adminServerProperties.getContextPath();
    }
 
    @Bean
    public CustomAuthenticationManager customAuthenticationManager(UserDetailsService userDetailsService) {
        return new CustomAuthenticationManager(userDetailsService);
    }
 
    @Bean
    public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http, CustomAuthenticationManager customAuthenticationManager) {
        // @formatter:off
        RedirectServerAuthenticationSuccessHandler successHandler = new RedirectServerAuthenticationSuccessHandler();
        successHandler.setLocation(URI.create(contextPath + "/"));
        return http.headers().frameOptions().disable().and()
            .authorizeExchange()
            // 放开静态文件和登陆
            .pathMatchers(
                contextPath + "/assets/**"
                , contextPath + "/login"
                , contextPath + "/applications"
                , contextPath + "/v1/agent/**"
                , contextPath + "/v1/catalog/**"
                , contextPath + "/v1/health/**"
            ).permitAll()
            // 内网可访问 actuator
            .pathMatchers(contextPath + "/actuator", contextPath + "/actuator/**").access(new InternalAuthorizationManager())
            .anyExchange().authenticated().and()
            .formLogin().loginPage(contextPath + "/login")
            .authenticationSuccessHandler(successHandler)
            .authenticationManager(customAuthenticationManager).and()
            .logout().logoutUrl(contextPath + "/logout").and()
            .httpBasic().disable()
            .csrf().disable()
            .build();
        // @formatter:on
    }
 
}