Add reste entry point to spring security configuration.
This commit is contained in:
28
src/main/java/org/codiki/core/security/RestAuthenticationEntryPoint.java
Executable file
28
src/main/java/org/codiki/core/security/RestAuthenticationEntryPoint.java
Executable file
@@ -0,0 +1,28 @@
|
|||||||
|
package org.codiki.core.security;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Authentication entry point configured in
|
||||||
|
* {@link SecurityConfiguration#configure(org.springframework.security.config.annotation.web.builders.HttpSecurity)}
|
||||||
|
* to avoid yo get a login form at authentication failure from Angular app.
|
||||||
|
*
|
||||||
|
* @author takiguchi
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {
|
||||||
|
@Override
|
||||||
|
public void commence(HttpServletRequest request, HttpServletResponse response,
|
||||||
|
AuthenticationException authException) throws IOException, ServletException {
|
||||||
|
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -22,8 +22,11 @@ import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository;
|
|||||||
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
|
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
|
||||||
|
|
||||||
private static final String XSRF_REPOSITORY_HEADER_NAME = "X-XSRF-TOKEN";
|
private static final String XSRF_REPOSITORY_HEADER_NAME = "X-XSRF-TOKEN";
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private CustomAuthenticationProvider authenticationProvider;
|
private CustomAuthenticationProvider authenticationProvider;
|
||||||
|
@Autowired
|
||||||
|
private RestAuthenticationEntryPoint authenticationEntryPoint;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
||||||
@@ -50,6 +53,9 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
|
|||||||
).permitAll()
|
).permitAll()
|
||||||
.anyRequest().authenticated()
|
.anyRequest().authenticated()
|
||||||
.and()
|
.and()
|
||||||
|
// Allow to avoid login form at authentication failure from Angular app
|
||||||
|
.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint)
|
||||||
|
.and()
|
||||||
.addFilterAfter(new XSRFTokenFilter(), CsrfFilter.class)
|
.addFilterAfter(new XSRFTokenFilter(), CsrfFilter.class)
|
||||||
.csrf()
|
.csrf()
|
||||||
.csrfTokenRepository(xsrfTokenRepository());
|
.csrfTokenRepository(xsrfTokenRepository());
|
||||||
|
|||||||
Reference in New Issue
Block a user