在spring boot中从jwt安全检查中排除url

5w9g7ksd  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(499)

我有一个spring引导应用程序,通过将这些依赖项添加到pom.xml中,我用资源服务器来保护它。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-oauth2-jose</artifactId>
</dependency>

这通常工作得很好,但是我需要从安全检查中排除特定的url,我尝试通过创建自定义的websecurityconfigureradapter来实现这一点。

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class JWTSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(final WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/test");
    }
}

但是,在创建这个类之后,所有调用(除了to/test调用)都将失败,因为服务器将重定向到登录页。
我的端点如下所示:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/")
public class TestController {

    @GetMapping("test") // This endpoint should be ignored
    public String test() {
        return "1.0";
    }

    @GetMapping("foo")
    public String test1() {
        return "foo";
    }

}

请求时http://localhost:8080/测试我的日志输出如下所示:

2020-08-24 08:48:28.215 DEBUG 7473 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : GET "/test", parameters={}
2020-08-24 08:48:28.215 DEBUG 7473 --- [nio-8080-exec-5] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.test.controllers.TestController#test()
2020-08-24 08:48:28.218 DEBUG 7473 --- [nio-8080-exec-5] m.m.a.RequestResponseBodyMethodProcessor : Using 'text/html', given [text/html, application/xhtml+xml, image/webp, image/apng, application/xml;q=0.9, application/signed-exchange;v=b3;q=0.9, */*;q=0.8] and supported [text/plain, */*, text/plain, */*, application/json, application/*+json, application/json, application/*+json]
2020-08-24 08:48:28.218 DEBUG 7473 --- [nio-8080-exec-5] m.m.a.RequestResponseBodyMethodProcessor : Writing ["1.0"]
2020-08-24 08:48:28.219 DEBUG 7473 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : Completed 200 OK

到达终点http://localhost:8080/foo将导致重定向到登录页,并且根本不会有日志输出。
有人能告诉我我错过了什么吗?我怎样才能创建一个websecurityconfigureradapter,它除了从安全检查中排除一些url之外什么都不做?
请在此处查找虚拟项目:https://github.com/paulsasel/spring-boot-jwt-exclude-urls

bzzcjhmw

bzzcjhmw1#

如果你忽略了 void configure(final WebSecurity web) 然后它完全忽略了来自过滤器链的请求。请参阅spring安全配置-httpsecurity与websecurity
但这只是第一部分。还有一个过载 void configure(HttpSecurity http) 它将在链的后面调用,并定义您希望如何保护端点。如果不重写它,默认值将是formlogin,您可以在源中看到它:

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

这就是为什么它会将您重定向到登录页面。
因此,为了使用jwt身份验证,您也应该覆盖后者,并定义要使用oauth2资源服务器进行身份验证:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.cors()
            .and()
            .authorizeRequests()
                .anyRequest()
                    .authenticated()
            .and()
                .oauth2ResourceServer()
                    .jwt();
}

顺便说一下,这里还可以检查角色或请求类型。此外,作为一种替代方法,您也可以忽略/test端点,这对我来说是一个更干净的选择:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.cors()
            .and()
            .authorizeRequests()
                .antMatchers("/test")
                    .permitAll()
                .antMatchers(HttpMethod.GET, "/foo")
                    .hasAuthority("DUMMYAUTHORITY")
                .anyRequest()
                    .authenticated()
            .and()
                .oauth2ResourceServer()
                    .jwt();
}

伯南多

相关问题