gpt4 book ai didi

java - Spring security antMatcher 不起作用

转载 作者:行者123 更新时间:2023-12-01 18:31:40 26 4
gpt4 key购买 nike

编辑:

我进一步深入研究了问题,发现即使使用单一配置,问题仍然存在。如果我使用单一配置并保留

http.antMatcher("/api/test/**")

网址不安全。删除 antMatcher 和 antMatchers 会立即保护 url。即如果我使用:

http.httpBasic()
.and()
.authorizeRequests()
.anyRequest()
.authenticated();

那么只有 Spring Security 正在保护 url。为什么 antMatcher 无法运行?

(更新了标题以包含实际问题。)

<小时/>

原帖:

我提到了以下 stackoverflow 问题:

  1. Spring REST security - Secure different URLs differently

  2. Using multiple WebSecurityConfigurerAdapter with different AuthenticationProviders (basic auth for API and LDAP for web app)

和 Spring Security 文档:

https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#multiple-httpsecurity

但我无法配置多个 http 安全元素。当我遵循官方 spring 文档时,它在我的情况下才有效,因为第二个 http 安全元素是包罗万象的,但是一旦我添加特定的 url,就可以访问所有 url,无需任何身份验证。

这是我的代码:

@EnableWebSecurity
@Configuration
public class SecurityConfig {

@Bean
public UserDetailsService userDetailsService() throws Exception {
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
manager.createUser(User.withUsername("user").password("userPass").roles("USER").build());
manager.createUser(User.withUsername("admin").password("adminPass").roles("ADMIN").build());
return manager;
}


@Configuration
@Order(1)
public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {

@Override
public void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth.inMemoryAuthentication().withUser("user").password("user").roles("USER");
auth.inMemoryAuthentication().withUser("admin").password("admin").roles("ADMIN");
}

protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/api/v1/**")
.authorizeRequests()
.antMatchers("/api/v1/**").authenticated()
.and()
.httpBasic();
}
}

@Configuration
@Order(2)
public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

@Override
public void configure(AuthenticationManagerBuilder auth)
throws Exception {

auth.inMemoryAuthentication().withUser("user1").password("user").roles("USER");
auth.inMemoryAuthentication().withUser("admin1").password("admin").roles("ADMIN");
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/api/test/**")
.authorizeRequests()
.antMatchers("/api/test/**").authenticated()
.and()
.formLogin();
}
}
}

现在任何网址都可以访问了。如果我从第二个配置中删除 antMatcher,所有网址都会受到保护。

最佳答案

该模式不得包含上下文路径,请参阅 AntPathRequestMatcher :

Matcher which compares a pre-defined ant-style pattern against the URL ( servletPath + pathInfo) of an HttpServletRequest.

HttpServletRequest.html#getServletPath :

Returns the part of this request's URL that calls the servlet. This path starts with a "/" character and includes either the servlet name or a path to the servlet, but does not include any extra path information or a query string. Same as the value of the CGI variable SCRIPT_NAME.

HttpServletRequest.html#getContextPath :

Returns the portion of the request URI that indicates the context of the request. The context path always comes first in a request URI. The path starts with a "/" character but does not end with a "/" character. For servlets in the default (root) context, this method returns "". The container does not decode this string.

您修改和简化的代码:

    @Override
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/test/**")
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin();
}

关于java - Spring security antMatcher 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60154544/

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