gpt4 book ai didi

java - Spring Security 忽略多个 HTTP 配置

转载 作者:行者123 更新时间:2023-12-01 11:46:05 24 4
gpt4 key购买 nike

我一直在使用 Spring Security 开发一个 Web 应用程序,该应用程序有一些复杂的要求(支持数据库和 LDAP 身份验证、登录页面和 API),除了一件事我无法得到之外,我基本上已经弄清楚了:处理多个http。我在 JAVA 中做所有事情,没有 web.xml。当 Spring 调试打开时,它可以识别所有三个,但它似乎只是将请求与第一个 http 进行比较,然后继续。这是我的代码:

AppConfig.java

@Configuration
@ComponentScan( { "com.mydomain.security" } )
@Import( { SecurityConfig.class } )
public class AppConfig{
...
}

SecurityConfig.java

@Configuration
@EnableWebSecurity
public class SecurityConfig{

@Autowired
Config cfg;

@Autowired
LdapContextSource ldapContextSource;

@Autowired
@Qualifier( "authenticationProviderDB" )
AuthenticationProvider authenticationProviderDB;

@Autowired
@Qualifier( "authenticationProviderLDAP" )
AuthenticationProvider authenticationProviderLDAP;

@Autowired
@Qualifier( "persistentRememberMeServices" )
static RememberMeServices persistentRememberMeServices;

@Autowired
@Qualifier( "tokenRepository" )
CustomTokenRepository tokenRepository;

@Autowired
public void configureGlobal( AuthenticationManagerBuilder auth )throws Exception{
auth.authenticationProvider( authenticationProviderLDAP );
auth.authenticationProvider( authenticationProviderDB );
}

@Configuration
@Order( 1 )
public static class RestWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter{
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers( "/api/**" ).hasRole("ROLE_USER").and().httpBasic()
.and().exceptionHandling().accessDeniedPage( "/security/api" )
}
}

@Configuration
public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter{
@Override
protected void configure( HttpSecurity http )throws Exception{
http.authorizeRequests().antMatchers( "/**" ).hasRole("ROLE_USER")
.and().formLogin().loginPage( "/security/login" ).failureUrl( "/security/login?error" ).usernameParameter( "username" ).passwordParameter( "password" )
.loginProcessingUrl("/j_spring_valuehere")
.and().rememberMe().rememberMeServices( persistentRememberMeServices ).key( "key" )
.and().addFilterBefore( (Filter)new CustomErrorHandlerFilter(), RememberMeAuthenticationFilter.class)
.csrf().disable();
}
}
}

日志

8075 [http-bio-8080-exec-3] DEBUG org.springframework.security.web.FilterChainProxy  - /index.jsp at position 10 of 10 in additional filter chain; firing Filter: 'FilterSecurityInterceptor'
8076 [http-bio-8080-exec-3] DEBUG org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/index.jsp'; against '/security/**'
8076 [http-bio-8080-exec-3] DEBUG org.springframework.security.web.access.intercept.FilterSecurityInterceptor - Public object - authentication not attempted
8077 [http-bio-8080-exec-3] DEBUG org.springframework.security.web.FilterChainProxy - /index.jsp reached end of additional filter chain; proceeding with original chain

非常感谢对我可能缺少的内容的任何帮助。

最佳答案

尝试改变

http.authorizeRequests().antMatchers( "/api/**" ).hasRole("ROLE_USER").and().httpBasic()
.and().exceptionHandling().accessDeniedPage( "/security/api" )

http.antMatcher("/api/**").authorizeRequests().antMatchers( "/api/**" ).hasRole("ROLE_USER").and().httpBasic()
.and().exceptionHandling().accessDeniedPage( "/security/api" )

这将覆盖您设置的 /** 链。

关于java - Spring Security 忽略多个 HTTP 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29114119/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com