如何解决使用 oAuth2.0 和表单登录的 Spring Security
在我的 Spring Boot 应用程序中,我使用 Google 进行基于表单的登录和基于 oAuth2.0 的登录,我的安全配置定义为
http
.cors()
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.csrf().disable()
.authorizeRequests()
.antMatchers("/oauth2/**").permitAll()
.anyRequest().authenticated()
.and()
.addFilter(new JWTAuthenticationFilter(authenticationManager()))
.addFilter(jwtAuthorizationFilter)
.formLogin()
.and()
.oauth2Login()
.authorizationEndpoint()
.authorizationRequestRepository(httpCookieOAuth2AuthorizationRequestRepository)
.and()
.userInfoEndpoint()
.userService(customOAuth2UserService)
.and()
.successHandler(oAuth2AuthenticationSuccessHandler)
.failureHandler(oAuth2AuthenticationFailureHandler);
我正在使用 JWT 进行无状态身份验证,对于基于表单的登录 JWTAuthenticationFilter
处理身份验证如下。
JWTAuthenticationFilter
public class JWTAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
//code to authenticate user and generate JWT
}
和 JWTAuthorizationFilter
为后续请求验证 JWT,如下所示。
JWTAuthorizationFilter
public class JWTAuthorizationFilter extends BasicAuthenticationFilter {
// code to perform authorization
}
现在来到 OAuth2.0 customOAuth2UserService
确实会注册新用户并更新现有用户信息。
OAuth2AuthenticationSuccessHandler
在成功的 OAuth2.0 身份验证上生成 JWT。
现在,表单登录和 OAuth2.0 的功能都很好,但我正在尝试解决一些需要帮助的小问题。
问题
- 如果我使用错误的表单登录凭据点击
http://localhost:8080/login
,我会收到一个 HTML 表单,其中 Google 登录作为响应正文,200 作为状态代码,理想情况下我想要 400 个错误请求或其他内容。 - 当我不随请求提供 JWT 令牌时,我会收到带有 200 状态代码的相同 HTML 响应正文,理想情况下我也需要 401。
我缺少哪些配置以及我可以更改哪些配置来解决这些问题。
解决方法
对于第二个问题,您可以添加 这是 HTTP 安全配置
.exceptionHandling().authenticationEntryPoint(jwtUnAuthorizedResponseAuthenticationEntryPoint)
然后在类中自动装配 jwtUnAuthorizedResponseAuthenticationEntryPoint 并像这样定义 JwtUnAuthorizedResponseAuthenticationEntryPoint 类
import java.io.IOException;
import java.io.Serializable;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;
@Component
public class JwtUnAuthorizedResponseAuthenticationEntryPoint implements AuthenticationEntryPoint,Serializable {
// they is always a generic message that is showed when the unauthenticated user
// user makes a request
// here in this class we will override and show what we want to show to an
// unauthorized user
// this is called internally
private static final long serialVersionUID = -8970718410437077606L;
@Override
public void commence(HttpServletRequest request,HttpServletResponse response,AuthenticationException authException) throws IOException {
response.sendError(HttpServletResponse.SC_UNAUTHORIZED,"You would need to provide the Jwt Token to Access This resource");
}
}
它会抛出你想要的 401 错误
public static final int SC_UNAUTHORIZED = 401;
在public interface HttpServletResponse extends ServletResponse
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。