oauth 認證伺服器 配置HandlerInterceptorAdapter

exception發表於2019-01-19

編寫LoginHandlerInterceptor
@Configuration
public class LoginHandlerInterceptor extends HandlerInterceptorAdapter {

private static final Logger logger = LoggerFactory.getLogger(LoginHandlerInterceptor.class);

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    return super.preHandle(request, response, handler);
}

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    logger.debug("modelAndView{}", modelAndView);
    logger.debug("handler{}", handler);
    logger.debug("response{}", response);
    logger.debug("request{}", request);

    // 獲取ip
    String ipAddr = RequestIpUtil.getIpAddress(request);

    super.postHandle(request, response, handler, modelAndView);
}

}
@Autowired
private LoginHandlerInterceptor loginHandlerInterceptor;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {

endpoints.tokenStore(getRedisTokenStore())
        .userDetailsService(ssoUserDetailsService)
        // 如果這個不寫著 會報Unsupported grant type: password
        .authenticationManager(authenticationManager)
        .addInterceptor(loginHandlerInterceptor)
        .exceptionTranslator(customWebResponseExceptionTranslator);
        }

相關文章