Java Spring boot返回401-未经授权,即使我允许
我正试图通过邮递员访问我的API。在配置中,我已设置/验证为允许,但我一直未经授权 有关职能:Java Spring boot返回401-未经授权,即使我允许,java,spring,spring-boot,authentication,postman,Java,Spring,Spring Boot,Authentication,Postman,我正试图通过邮递员访问我的API。在配置中,我已设置/验证为允许,但我一直未经授权 有关职能: @Override protected void configure(HttpSecurity httpSecurity) throws Exception { httpSecurity // dont authenticate this particular request .authorizeRequests(
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
// dont authenticate this particular request
.authorizeRequests().antMatchers("/authenticate").permitAll().
// all other requests need to be authenticated
anyRequest().authenticated().and().
// make sure we use stateless session; session won't be used to
// store user's state.
exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint).and().sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
// Add a filter to validate the tokens with every request
httpSecurity.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
}
我的依赖关系
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>2.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
</dependencies>
org.springframework.boot
spring引导启动器数据jpa
org.springframework.boot
SpringBootStarterWeb
org.springframework.boot
弹簧靴开发工具
运行时
真的
org.postgresql
postgresql
运行时
org.springframework.boot
弹簧起动试验
测试
org.springframework.boot
弹簧启动安全
2.3.1.1发布
io.jsonwebtoken
jjwt
0.9.1
任何帮助都将不胜感激这里的问题是,即使您将Spring Security配置为允许每个用户使用,您的请求也将通过与其他请求相同的安全链。我猜您对身份验证端点的调用在授权头中不包含任何内容,或者换句话说,您的令牌是空的,因此您得到的是401。在这种情况下,一种解决方案是对spring security说,对于用于身份验证的URL,请求不会通过安全链传递。您可以通过Spring安全配置中的以下代码实现这一点
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/authenticate");
}
我希望这可以解决您的问题。这里的问题是,即使您将Spring Security配置为允许每个用户使用,您的请求也会通过与其他请求相同的安全链。我猜您对身份验证端点的调用在授权头中不包含任何内容,或者换句话说,您的令牌是空的,因此您得到的是401。在这种情况下,一种解决方案是对spring security说,对于用于身份验证的URL,请求不会通过安全链传递。您可以通过Spring安全配置中的以下代码实现这一点
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/authenticate");
}
我希望这可以解决您的问题。通常,您希望将认证URL从安全检查中排除。有关更多详细信息,请检查:同时确保过滤器工作正常。例如,尝试通过请求另一个资源来验证用户身份,而不是使用有效的令牌进行验证。有时这种情况会由于csrf和cors的问题而发生,请尝试添加
.csrf().disable().cors()
到您的httpSecurityobject@PabloAndrésRodasGiraldo感谢您的建议,但我仍然得到相同的结果,通常您希望从安全检查中排除身份验证URL。有关更多详细信息,请检查:同时确保过滤器工作正常。例如,尝试通过请求另一个资源来验证用户身份,而不是使用有效的令牌进行验证。有时这种情况会由于csrf和cors问题而发生,请尝试将.csrf().disable().cors()
添加到您的httpSecurity中object@PabloAndrésRodasGiraldo谢谢你的建议,但我还是得到了同样的结果