Java Spring安全HttpSecurity配置

Java Spring安全HttpSecurity配置,java,spring,spring-boot,spring-security,spring-oauth2,Java,Spring,Spring Boot,Spring Security,Spring Oauth2,我试图理解RequestMatcher、AntMatcher等是如何工作的。我读了一些帖子,了解了一些基本知识。实际上,我有一个简单的基本配置: @Override protected void configure(HttpSecurity http) throws Exception { http.requestMatchers() //1 .antMatchers("/login", "/oauth/authorize") //2 .and() //3

我试图理解RequestMatcher、AntMatcher等是如何工作的。我读了一些帖子,了解了一些基本知识。实际上,我有一个简单的基本配置:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.requestMatchers() //1
        .antMatchers("/login", "/oauth/authorize") //2
        .and() //3
        .authorizeRequests() //4
        .anyRequest() //5
        .authenticated() //6;
我真的不明白第1点、第2点和第3点。根据我的理解,这意味着
/login
/oauth/authorize
的请求被映射,应该是授权请求。所有其他请求都需要身份验证

端点
/user/me
的意思是必须对我进行身份验证,因为它受第5点和第6点的控制? 对该端点的调用对我有效

在ohter配置中,我尝试了不同的方法:

@Override
protected void configure(HttpSecurity http) throws Exception { // @formatter:off
      http
       .authorizeRequests() //1
        .antMatchers("/login", "/oauth/authorize", "/img/**").permitAll() //2
        .anyRequest() //3
        .authenticated() //4
在我看来,这应该是与第一个配置相同的逻辑。但是实际上端点
/user/me
不再是可访问的

我非常感谢您的澄清


更新1:

这是我现在的配置:

@Override
protected void configure(HttpSecurity http) throws Exception { // @formatter:off
    http
        .requestMatchers()
           .antMatchers("/", "/login", "/oauth/authorize", 
               "/main", "/logout-success", "/single-logout",
               "/password_forgotten", "/enter_new_password", "/img/**",
               "/logout", "/access_denied")
            .and().authorizeRequests()
                .antMatchers("/img/**", "/logout-success", "/password_forgotten",
                    "/enter_new_password", "/access_denied").permitAll()
            .requestMatchers(SecurityUtils::isFrameworkInternalRequest).permitAll()
            .and()
            .authorizeRequests()
            .anyRequest()
            .authenticated()
            .and()
            .formLogin()
            .loginPage("/login")
            .failureUrl("/login?error")
            .defaultSuccessUrl("/main")
            .permitAll()
            .and()
            .logout()
            .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
            .logoutSuccessUrl("/logout-success")
            .deleteCookies("JSESSIONID")
            .invalidateHttpSession(true)
            .and()
            .exceptionHandling()
            .accessDeniedPage("/access_denied")
            .authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/login"))
            .and().csrf().disable();
如果我以未经身份验证的用户身份输入URL
\user\me
,我会得到一个401,并显示以下消息:

<oauth>
<error_description>
Vollständige Authentifikation wird benötigt um auf diese Resource zuzugreifen
</error_description>
<error>unauthorized</error>
</oauth>

沃尔斯特州的权威人士认为这是一种资源
未经授权

这是可以的,但意味着任何其他安全过滤器链都会针对此URL发生,对吗?

您的第一个配置
.requestMtchers()//1

@覆盖
受保护的无效配置(HttpSecurity http)引发异常{
http.requestMatchers()//1
.antMatchers(“/login”,“/oauth/authorize”)//2
.和()//3
.authorizeRequests()//4
.anyRequest()//5
.authenticated()//6;
让我解释一下你的
.authorizeRequests()
/4

http.authorizeRequests()
这是一张通配符(
/**
)(就像筛选器允许每个请求一样)
HTTP-安全配置只考虑这些模式的请求 在这里你可以说

http.authorizeRequests()
//等于
http.antMatcher(“/**”).authorizeRequests();
//也等于
http.requestMatchers()
.antMatchers(“/**”)
.及()
.授权请求()
如果您像下面这样配置

http.antMatcher("/api/**").authorizeRequests();
只有在传入请求uri与配置的antmatcher匹配时(
.hasRole()
.hasAnyRole
.authenticated()
.authenticated()
)才会查询其余配置(
/api/**

假设您需要配置多个URL(不同的模式),那么您不能只使用一个antMatcher。您需要多个,然后您应该使用requestMatcher,如下所示

http.requestMatchers()
.antMatchers(“/api/**”、“员工/**”、“客户/**”)
.及()
.授权请求()
.antMatchers()//2
用于配置
requestMatchers配置程序
的返回类型
.requestMatchers()

或者也
用于配置
ExpressionInterceptUrlRegistry
的返回类型
.authorizeRequests()

.and()//3

返回HttpSecurity以进行进一步定制

.anyRequest()/5

创建RequestMatcher后链接的对象

所有请求匹配配置的请求匹配器模式

.authenticated()//6
下面的配置是自我解释的

.antMatchers("/app/admin/**").hasRole("ADMIN")
//or
.antMatchers("/app/admin/**").hasAnyRole("ADMIN", "USER")
//or
.antMatchers("/app/admin/**").authenticated()
//or
.antMatchers("/app/admin/**").permitAll()
这是关于antMatchers、authorizeRequests和authenticated的粗略概念。您可以在这个链接中参考我的答案
配置URL是否将由该
SecurityFilterChain
处理。因此,如果URL与之不匹配,整个
SecurityFilterChain
将被跳过,这意味着Spring Security在此之后将不处理此URL。如果不配置它,默认情况下将匹配所有URL

authorizeRequests()
为URL配置授权内容,例如是否需要对其进行身份验证,或者是否只有某些角色可以访问它等。它仅对由该SecurityFilterChain处理的URL有效(即由
requestMatchers()匹配的URL

那么,回到您的第一个示例:

http.requestMatchers()//1
.antMatchers(“/login”,“/oauth/authorize”)//2
.和()//3
.authorizeRequests()//4
.anyRequest()//5
.authenticated()//6;
这意味着此SecurityFilterChain仅对
/login
/oauth/authorize
有效。这两个URL都需要经过身份验证。所有其他URL都不会由此SecurityFilterChain处理。因此,是否需要对
/user/me
进行身份验证与Spring Security无关

http
.authorizeRequests()//1
.antMatchers(“/login”、“/oauth/authorize”、“/img/**”).permitAll()//2
.anyRequest()//3
.authenticated()//4

这意味着所有URL都将由此SecurityFilterChain处理(默认值为
requestMatchers()
).
/login
/oauth/authorize
/img/**
不需要任何授权。其他URL需要进行身份验证。

非常感谢您的澄清。例如,1它表示
授权请求()
任何请求()
只属于之前定义的AntMatcher,不影响其他URL。这意味着
/login
也需要身份验证。因此,如果我第一次输入此URL,spring会自动让我成为匿名用户,直到我真正进行身份验证,对吗?
/user/me
到位的原因是此资源是如果我未经授权,则会真正安全(在示例1中)。因此,必须有另一个securityFilterChain检查此url,对吗?是的。你是对的。可能是另一个
securityFilterChain
处理了
/user/me
。我建议