Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从Spring Boot中的JWT安全检查中排除URL_Java_Spring Boot_Jwt - Fatal编程技术网

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

Java 从Spring Boot中的JWT安全检查中排除URL,java,spring-boot,jwt,Java,Spring Boot,Jwt,我有一个Spring引导应用程序,通过将这些依赖项添加到pom.xml中,我使用资源服务器来保护它 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-resource-server</artifactId> </dependency> <dependency>

我有一个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> 
但是,在创建此类之后,所有调用(除了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";
    }

}
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/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";
    }

}
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 将导致重定向到登录页面,并且根本不会有日志输出

谁能告诉我我错过了什么?我如何创建一个WebSecurity配置适配器,它除了从安全检查中排除一些URL之外,什么都不做


请在此处找到一个虚拟项目:

如果您忽略了
void configure(final WebSecurity web)
中的某个内容,那么它将完全忽略来自过滤器链的请求。看

但这只是第一部分。还有另一个重载
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();
}
比尔,
Nandor

在重定向发生时,您是否可以在日志中包含您试图调用的端点以及您收到的任何其他信息?仅对于“双重检查”,尝试使用自定义的
@覆盖受保护的无效配置(HttpSecurity http)抛出异常(在
JWTSecurityConfig
内部)将日志级别的安全设置为调试(application.properties add line
logging.level.org.springframework.security=DEBUG
),并检查是否忽略测试端点,您应该会看到类似于
o.s.security.web.FilterChainProxy:/test的内容有一个空的筛选器列表
Hi,谢谢您的评论。我编辑了问题并添加了日志信息。我还尝试将以下方法添加到JWTSecurityConfig中:受保护的void configure(最终HttpSecurity http)引发异常{http.authorizeRequests().antMatchers(“/test”).permitAll().anyRequest().authorized();}在这种情况下,当点击/foo端点时,我会得到一个403错误页面,但当你在所有端点上激活spring security基本身份验证时,当点击端点时,我也会得到403错误页面,默认情况下使用有效的JWT令牌。所有你要访问的端点都会将你重定向到默认的登录页面。您已选择忽略
/test
端点的所有spring安全性,以便一个端点通过。你期望的行为是什么?使用JWTs?那么,您需要禁用基本登录,并配置jwts的使用。如果您有Github帐户(或任何其他类似帐户),最好的选择是将您的项目包含在其中,这样,任何人都可以下载它,并以更好的方式帮助您了解您的案例中缺少/需要的内容。