Last active
September 30, 2018 22:27
-
-
Save m1k3yfoo/ac14be7534a9db6f574cc7e618770a2c to your computer and use it in GitHub Desktop.
[Java Spring Boot AuthenticationEntryPoint] #SpringBoot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.project; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
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.authentication.www.BasicAuthenticationEntryPoint; | |
import org.springframework.stereotype.Component; | |
@Component | |
public class AuthenticationEntryPoint extends BasicAuthenticationEntryPoint { | |
@Override | |
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authEx) | |
throws IOException, ServletException { | |
response.addHeader("LoginUser", "Basic " +getRealmName()); | |
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); | |
PrintWriter writer = response.getWriter(); | |
writer.println("HTTP Status 401 - " + authEx.getMessage()); | |
} | |
@Override | |
public void afterPropertiesSet() throws Exception { | |
setRealmName("springboot"); | |
super.afterPropertiesSet(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment