Spring boot安全-如何使用SecurityFilterChain进行身份验证?

问题描述 投票:0回答:1

我正在尝试制作一个使用以下内容的 Web 应用程序:SpringBoot、Mysql、JDBC、MVC、DAO Thymeleaf 和 IntelliJ。

我正在尝试弄清楚 Spring 安全性是如何工作的(对此我遇到了很多困难)。 我之前曾在这篇文章中寻求帮助: 如何为Spring Boot项目配置Spring Security 有人向我解释说我不应该使用

WebSecurityConfigurerAdapter
类,因为它已被弃用,而是使用
SecurityFilterChain

@Configuration
public class WebSecurityConfig
{
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception
    {
        // @formatter:off
        http.authorizeRequests()
            .mvcMatchers("/userOnly/**").permitAll()
            .anyRequest().permitAll();
        http.formLogin()
            .permitAll()
            .loginPage("/loginPage.html");
        http.logout()
            .permitAll();
        // @formatter:on
        
        return http.build();
    }
}

它运行完美,我再次感谢 Ralan 的帮助 但这样做我不知道如何使用 JDBC 和数据库 MySQL 中包含我的用户的表进行身份验证。

抱歉,如果我的问题看起来很奇怪,但英语不是我的母语

java spring spring-boot spring-mvc spring-security
1个回答
0
投票

是的,WebSecurityConfigurerAdapter 类已被弃用,SecurityFilterChain 取代了它。这是我们如何使用它的。

@Bean
public SecurityFilterChain configure(final HttpSecurity http) throws Exception {
    http.csrf(AbstractHttpConfigurer::disable)
            .headers(this::enableXssProtection).
            sessionManagement(httpSecuritySessionManagementConfigurer ->
                    httpSecuritySessionManagementConfigurer.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED))
            .authorizeHttpRequests(authorize -> authorize
                    .requestMatchers("/token", "/health", "/info")
                    .permitAll()
                    .requestMatchers("/loggers", "/loggers/*")
                    .hasAuthority("ROLE_ADMIN")
                    .anyRequest().authenticated())
            .oauth2ResourceServer(oauth2 -> oauth2.opaqueToken(token ->
                    token.authenticationConverter((introspectedToken, authenticatedPrincipal) -> {
                                final Instant issuedAt = authenticatedPrincipal.getAttribute("iat");
                                final Instant expiresAt = authenticatedPrincipal.getAttribute("exp");

                                Collection<? extends GrantedAuthority> authorities = AuthorityUtils.NO_AUTHORITIES;
                                final List<String> extractedAuthorities = authenticatedPrincipal.getAttribute("authorities");
                                if (extractedAuthorities != null) {
                                    authorities = AuthorityUtils.createAuthorityList(extractedAuthorities.toArray(String[]::new));
                                }
                                return new BearerTokenAuthentication(authenticatedPrincipal,
                                        new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, introspectedToken, issuedAt, expiresAt),
                                        authorities);
                            })
                            .introspectionUri(checkTokenUrl)
                            .introspectionClientCredentials(clientId, clientSecret)));
    return http.build();
}

private void enableXssProtection(HeadersConfigurer<HttpSecurity> httpSecurityHeadersConfigurer) {
    httpSecurityHeadersConfigurer.xssProtection(xXssConfig -> xXssConfig.headerValue(ENABLED_MODE_BLOCK));
}

我们使用

http.csrf(AbstractHttpConfigurer::disable)
来禁用 CSS 保护,但请注意,如果您的应用程序正在提供 HTML 页面(用户在其中提交表单),那么您不应该使用它。我们还使用自定义方法
headers(this::enableXssProtection)
来配置 HTTP 标头,以防止跨站点脚本 (XSS) 攻击。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.