随笔 - 836  文章 - 1 评论 - 40 阅读 - 102万
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

参考: https://geocolumbus.github.io/HTTPS-ELB-AWS-Spring-Boot/

 

1.  在服务器端配置  证书 域名 映射

 

2. 导入依赖:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
</dependency>

3.配置

复制代码
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private LdapConfig ldapConfig;

    @Autowired
    private CorsConfig corsConfig;

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Value("${security.https.path}")
    private String httpsPath;   // 项目路径  ,正式环境  配置 "/" 即可

 

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .requiresChannel().antMatchers(httpsPath).requiresSecure()
                .and()
                .authorizeRequests()
                //.antMatchers("/ui/**").fullyAuthenticated()
                //.antMatchers("/file/**").fullyAuthenticated()
                .antMatchers("/**").permitAll()
                .and().cors()
                .and().csrf().disable();
    }

    
}
复制代码

(备份)

复制代码
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private LdapConfig ldapConfig;

    @Autowired
    private CorsConfig corsConfig;

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Value("${security.https.path}")
    private String httpsPath;

    @Bean
    public UserDetailsContextMapper userDetailsContextMapper() {
        return new LdapUserDetailsMapper() {
            @Override
            public UserDetails mapUserFromContext(DirContextOperations ctx, String username,
                                                  Collection<? extends GrantedAuthority> authorities) {
                UserDetails details = super.mapUserFromContext(ctx, username, authorities);
                return new UserDetail((LdapUserDetails) details);
            }
        };
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .requiresChannel().antMatchers(httpsPath).requiresSecure()
                .and()
                .authorizeRequests()
                .antMatchers("/ui/**").fullyAuthenticated()
                .antMatchers("/file/**").fullyAuthenticated()
                .antMatchers("/**").permitAll()
                .and().cors()
                .and().csrf().disable();
    }

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .ldapAuthentication()
                .userDetailsContextMapper(userDetailsContextMapper())
                .userDnPatterns("uid={0},ou=people")
                .groupSearchBase("ou=groups")
                .contextSource()
                .url(ldapConfig.getUrl()+ldapConfig.getBase_dc())
                .managerDn(ldapConfig.getUsername())
                .managerPassword(ldapConfig.getPassword());
    }

    @Bean
    CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOrigins(corsConfig.getAllowedOrigins());
        configuration.setAllowedMethods(corsConfig.getAllowedMethods());
        configuration.setAllowedHeaders(corsConfig.getAllowedHeaders());
        configuration.setAllowCredentials(corsConfig.getAllowedCredentials());
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }
}
View Code
复制代码

 

4.在application. yml 或者 application.properties 中配置:

复制代码
server:
  port: 7000
  servlet:
    session:
      timeout: 1800
  tomcat:
    max-threads: 10
    remote-ip-header: x-forwarded-for
    protocol-header: x-forwarded-proto
复制代码

 

posted on   lshan  阅读(216)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示