我正在尝试在我的WebSecurityConfig配置方法中实现Spring Boot2中的自定义AuthenticationEntryPoint。
我见过很多这样的例子:
@Component
public class Http401UnauthorizedEntryPoint implements AuthenticationEntryPoint {
private final Logger log = LoggerFactory.getLogger(Http401UnauthorizedEntryPoint.class);
/**
* Always returns a 401 error code to the client.
*/
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException arg2) throws IOException,
ServletException {
log.debug("Pre-authenticated entry point called. Rejecting access");
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Access Denied");
}
}但我总是得到403分而不是401分。
我如何在Spring boot2中做到这一点?
发布于 2018-10-25 18:24:48
看看我的答案:https://stackoverflow.com/a/52986942/3437868和https://stackoverflow.com/a/52986779/3437868
简而言之:从Spring Boot2开始,Http401AuthenticationEntryPoint类已经被移除(参见Spring Boot Issue 10725)。
使用带有HttpStatus.UNAUTHORIZED的HttpStatusEntryPoint而不是Http401AuthenticationEntryPoint:
http.exceptionHandling()
.authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED));发布于 2018-10-17 17:26:40
我使用的是Spring boot 1.5,也许它不适合这种情况,但我仍然希望这能对你有所帮助。
在创建Http401UnauthorizedEntryPoint bean之后,您应该将其配置到Spring Security中。
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/api/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().disable()
.httpBasic().disable()
.logout().disable()
.csrf().disable()
// look here,bro
.exceptionHandling()
.authenticationEntryPoint(new Http401AuthenticationEntryPoint(""))
;
}那么入口点应该可以工作了。
实际上,Spring已经为您提供了许多入口点beans.Just,如"Http401AuthenticationEntryPoint“,它们也可以解决您自己创建bean的即时问题。
发布于 2018-10-29 17:44:39
您可以创建一个扩展WebSecurityConfigurerAdapter的类。然后从AuthenticationEntryPoint实现自动连接的Http401UnauthorizedEntryPoint类。您可以参考以下内容:
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private Http401UnauthorizedEntryPoint authenticationEntryPoint;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.exceptionHandling()
.authenticationEntryPoint(authenticationEntryPoint)
}
}https://stackoverflow.com/questions/49937472
复制相似问题