我已经苦苦挣扎了好几天了.我是Spring Boot的新手,并且喜欢不使用XML配置的想法.
我创建了一个RESTfull应用程序(使用JSON).我正在按照本教程正确配置身份验证.
我认为我设法使用Java配置重现几乎所有的配置,除了一件事 - AuthenticationEntryPoint
本教程使用http像这样的标记中的属性,并按以下方式定义formLogin:
<http entry-point-ref="restAuthenticationEntryPoint">
<intercept-url pattern="/api/admin/**" access="ROLE_ADMIN"/>
<form-login
authentication-success-handler-ref="mySuccessHandler"
authentication-failure-handler-ref="myFailureHandler"
/>
<logout />
</http>
Run Code Online (Sandbox Code Playgroud)
Spring Security手册中的AuthenticationEntryPoint解释说:
可以使用<http>元素上的entry-point-ref属性设置AuthenticationEntryPoint.
没有提到有关如何使用Java配置执行此操作的任何内容.
那么如何restAuthenticationEntryPoint在没有XML 的情况下"注册"我自己的,以防止在使用formLogin时重定向到登录表单?
下面我将提到我尝试过的内容.
谢谢你们.
在我的尝试中,发现你可以使用basicAuth定义它,如下所示:
@Configuration
@Order(1)
public static class RestWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
if (restAuthenticationEntryPoint == null) {
restAuthenticationEntryPoint = new RestAuthenticationEntryPoint();
}
http
.authorizeRequests()
.antMatchers("/**").hasAnyRole(Sec.ADMIN,Sec.SUPER_USER)
...
.and()
.httpBasic()
.authenticationEntryPoint(restAuthenticationEntryPoint)
Run Code Online (Sandbox Code Playgroud)
但我正在使用这样的表单登录(没有 …
随着春天引导 1.5.6.RELEASE我能够发送HTTP状态代码401,而不是403在描述如果请求的uri没有认证如何让春天的安全响应未经授权(HTTP 401码),这样做:
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//...
http.exceptionHandling()
.authenticationEntryPoint(new Http401AuthenticationEntryPoint("myHeader"));
//...
}
}
Run Code Online (Sandbox Code Playgroud)
使用org.springframework.boot.autoconfigure.security.Http401AuthenticationEntryPoint班级.
我刚刚升级到Spring Boot 2.0.0.RELEASE,发现不再有这样的类了(至少在那个包中).
问:Http401AuthenticationEntryPoint Spring Boot中
是否存在此类()?如果不是,那么在现有项目中保持相同行为以保持与依赖于此状态代码(401而不是403?)的其他实现保持一致的可能是一个很好的选择.
java spring spring-security http-status-code-401 spring-boot