package com.kidgrow.authclient.config;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.kidgrow.common.utils.ResponseUtil;
|
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.annotation.Bean;
|
import org.springframework.http.HttpStatus;
|
import org.springframework.security.access.AccessDeniedException;
|
import org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler;
|
import org.springframework.security.oauth2.provider.expression.OAuth2WebSecurityExpressionHandler;
|
import org.springframework.security.web.AuthenticationEntryPoint;
|
|
import javax.annotation.Resource;
|
import javax.servlet.ServletException;
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.IOException;
|
|
/**
|
* 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020<br>
|
*
|
* @Description: Security默认处理器<br>
|
* @Project: <br>
|
* @CreateDate: Created in 2020/2/13 11:14 <br>
|
* @Author: <a href="4345453@kidgrow.com">liuke</a>
|
*/
|
public class DefaultSecurityHandlerConfig {
|
@Resource
|
private ObjectMapper objectMapper;
|
|
/**
|
* 未登录,返回401
|
*
|
* @return
|
*/
|
@Bean
|
public AuthenticationEntryPoint authenticationEntryPoint() {
|
return (request, response, authException) -> ResponseUtil.responseWriter(objectMapper, response, authException.getMessage(), HttpStatus.UNAUTHORIZED.value());
|
}
|
|
@Bean
|
public OAuth2WebSecurityExpressionHandler oAuth2WebSecurityExpressionHandler(ApplicationContext applicationContext) {
|
OAuth2WebSecurityExpressionHandler expressionHandler = new OAuth2WebSecurityExpressionHandler();
|
expressionHandler.setApplicationContext(applicationContext);
|
return expressionHandler;
|
}
|
|
/**
|
* 处理spring security oauth 处理失败返回消息格式
|
*/
|
@Bean
|
public OAuth2AccessDeniedHandler oAuth2AccessDeniedHandler() {
|
return new OAuth2AccessDeniedHandler() {
|
|
@Override
|
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException authException) throws IOException, ServletException {
|
ResponseUtil.responseWriter(objectMapper, response, authException.getMessage(), HttpStatus.FORBIDDEN.value());
|
}
|
};
|
}
|
}
|