gpt4 book ai didi

java - 服务器未响应 Spring Security 的登录请求

转载 作者:行者123 更新时间:2023-12-01 17:36:05 32 4
gpt4 key购买 nike

我使用 Spring SecurityJWT 保护 REST API(我没有使用 Spring Boot)。

当我尝试向 REST API 发送身份验证请求 (/login) 时,我在 Postman 上得到无法获得任何响应

enter image description here

这是我的 JWT 过滤器

public class JwtAuthenticationFilter extends UsernamePasswordAuthenticationFilter {

...

public AuthenticationFilter(AuthenticationManager authenticationManager) {
super.setAuthenticationManager(authenticationManager);
}

@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
try {
... // getting the credentials from the request
return getAuthenticationManager().authenticate(new UsernamePasswordAuthenticationToken(credentials.login, credentials.password));
}
catch (IOException e) { throw new RuntimeException(e); }
}

@Override
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain,
Authentication authResult) throws IOException, ServletException {

... // generating the jwtToken;

response.setHeader("Authorization", jwtToken);
}
}

当我调试我的应用程序时,一切正常,并且执行 successfulAuthentication 方法,并且我在 header 请求中插入了正确的 token response.setHeader("Authorization", jwtToken); .

但之后,就像我的 REST API(或 Spring Security 或 Tomcat)不会发回任何响应!

这是安全配置:

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

...

@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.anyRequest().authenticated()
.and()
.addFilter(new JwtAuthenticationFilter(authenticationManager()));
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

}

...
}

对于除 /login 之外的其他 HTTP 请求,我在 Postman 中得到了 (403) HTML 响应,不是 JSON 响应。

<!doctype html>
<html lang="en">

<head>
<title>HTTP Status 403 – Forbidden</title>
...

那么为什么我的服务器没有响应 /login 请求?为什么 Spring security 不为所有 http 请求发送 JSON 响应?

/login 请求后记录:

DEBUG o.s.security.web.FilterChainProxy - /login at position 1 of 11 in additional filter chain; firing Filter: 'WebAsyncManagerIntegrationFilter'
DEBUG o.s.security.web.FilterChainProxy - /login at position 2 of 11 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter'
DEBUG o.s.security.web.FilterChainProxy - /login at position 3 of 11 in additional filter chain; firing Filter: 'HeaderWriterFilter'
DEBUG o.s.security.web.FilterChainProxy - /login at position 4 of 11 in additional filter chain; firing Filter: 'LogoutFilter'
DEBUG o.s.security.web.util.matcher.OrRequestMatcher - Trying to match using Ant [pattern='/logout', GET]
DEBUG o.s.security.web.util.matcher.AntPathRequestMatcher - Request 'POST /login' doesn't match 'GET /logout'
DEBUG o.s.security.web.util.matcher.OrRequestMatcher - Trying to match using Ant [pattern='/logout', POST]
DEBUG o.s.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/login'; against '/logout'
DEBUG o.s.security.web.util.matcher.OrRequestMatcher - Trying to match using Ant [pattern='/logout', PUT]
DEBUG o.s.security.web.util.matcher.AntPathRequestMatcher - Request 'POST /login' doesn't match 'PUT /logout'
DEBUG o.s.security.web.util.matcher.OrRequestMatcher - Trying to match using Ant [pattern='/logout', DELETE]
DEBUG o.s.security.web.util.matcher.AntPathRequestMatcher - Request 'POST /login' doesn't match 'DELETE /logout'
DEBUG o.s.security.web.util.matcher.OrRequestMatcher - No matches found
DEBUG o.s.security.web.FilterChainProxy - /login at position 5 of 11 in additional filter chain; firing Filter: 'JwtAuthenticationFilter'
DEBUG o.s.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/login'; against '/login'
DEBUG security.JwtAuthenticationFilter - Request is to process authentication
DEBUG o.s.security.authentication.ProviderManager - Authentication attempt using o.s.security.authentication.dao.DaoAuthenticationProvider
DEBUG o.s.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler - Creating new EntityManager for shared EntityManager invocation
Hibernate: select ...
DEBUG o.s.security.web.header.writers.HstsHeaderWriter - Not injecting HSTS header since it did not match the requestMatcher o.s.security.web.header.writers.HstsHeaderWriter$SecureRequestMatcher@5b319bff
DEBUG o.s.security.web.context.SecurityContextPersistenceFilter - SecurityContextHolder now cleared, as request processing completed

最佳答案

首先,我将讨论您得到的响应不是 Json 格式的(对于所有其他请求)。请注意,首先应分配 url 端点处的方法来生成您请求的内容。您想要的内容,您可以在 postman 的 json 标题中提及,它应该是 Accepts : Application/json

另一件事是服务器不响应您的登录请求的原因是,最好的配置应该适合您的情况。

http.csrf().disable()
//You can give the request that you want to allow
.anyRequest().authenticated()
.and()
.addFilter(new JwtAuthenticationFilter(authenticationManager()));

关于java - 服务器未响应 Spring Security 的登录请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61038156/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com