当前位置: 首页 > 知识库问答 >
问题:

从Spring Boot中的JWT安全检查中排除URL

乜建柏
2023-03-14

我有一个Spring Boot应用程序,通过将这些依赖项添加到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,我试图通过创建自定义的WebSecurityConfigureAdapter来实现这一点。

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)都将失败,因为服务器将重定向到登录页面。

我的endpoint如下所示:

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/test我的日志输出如下所示:

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

点击endpointhttp://localhost:8080/foo将导致重定向到登录页面,并且根本不会有日志输出。

有人能告诉我我错过了什么吗?我如何创建一个WebSecurityConfigureAdapter,它除了从安全检查中排除一些URL之外什么都不做?

请在这里找到一个虚拟项目:https://github.com/paulsasel/spring-boot-jwt-exclude-urls

共有1个答案

赫连淳
2023-03-14

如果忽略<code>void configure(最终web安全性web)

但这只是第一部分。还有另一个重载空配置(HttpSecurity超文本传输协议),它在链的后面被调用,并定义了您想要如何准确地保护endpoint。如果您不覆盖它,默认将是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();
}

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

@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();
}

Br, Nandor

 类似资料:
  • 通过系统内置规则,将匹配规则的安全性较低的资源扫描出来并按照建议进行处理,从而提高系统安全性的目的。 建议列表 建议列表显示所有匹配优化建议规则的资源列表,用户可根据建议对资源进行处理。 忽略列表 忽略列表显示不需要处理的资源或一类规则建议。 规则配置 规则配置即根据系统内影响资源安全的条件设置相应的规则,如安全组的规则设置等,当资源匹配规则则表示资源的安全性较低,需要用户进行处理等。

  • [错误]无法执行目标org.apache.maven.plugins:maven-checkstyle-plugin:2.17:项目上的check(default-cli)blynk:在checkstyle配置过程中失败:无法初始化模块BeforeExecutionExclusionFileFilter-无法实例化“BeforeExecutionExclusionFileFilter”类,也无法将

  • 所以我想对值中包含的值进行空安全检查。 所以我有三个相互包含的对象: Person有一个衣服对象,这个衣服对象有一个国家对象,这个国家对象有一个资本 所以一个人可能没有衣服,所以像这样的支票会抛出一个空指针: 如果路径上的任何对象为空,我如何使这样的语句返回false? 我也不想这样做。(如果可能的话,Java-8中的一行。

  • 我是IntelliJ和Gradle的新手,但我在Java和Eclipse方面有足够的经验。我从wsdl文件生成了一些java类。我将它们放在带有文件夹名称下 - “生成的源”。 我试图阻止此文件夹及其子文件夹的checkstyle,例如,并在IntelliJ上使用gradle。 我尝试了一些线路; 但我又失败了。 build.gradle; 编辑-在推荐之后(但仍然失败!): tasks.with

  • 更新 我想只排除本地项目,但仍然检索它们的传递依赖关系。我需要在Eclipse之外复制IvyDE工作区解析器的行为。 例 null null null

  • 我试图将查询与exclude查询一起使用,以便它匹配除要排除的术语之外的所有术语。我在一个基本的URI查询中找到了它,但不是常规的JSON查询。如何将此URI转换为JSON类型查询? 其中是不匹配的标记列表。 这是我目前所掌握的: 但是,当我这样做时,仍然包含在结果中。如何排除?