【发布时间】:2015-06-11 13:24:05
【问题描述】:
对于一个应用程序,我需要在 Spring 4.x 中创建一个安全外观。
这个薄层必须接受来自我们的移动应用程序的任何请求,并对提供的令牌(使用 openId 和 Oauth)执行安全检查。
验证成功后,需要将请求转发到后端应用程序,后端应用程序不需要了解安全令牌机制。
因此,流程将是这样的: security_facade_url/path/of/the/request
带有一个标头,指示在成功验证令牌时要调用的后端
验证成功后,安全外观会向后端 URL 发送请求 backend_application_url/path/of/the/request
外观不得具有映射到请求的任何可能路径的控制器,但必须根据请求标头中的值在正确的后端服务器上调用请求。然后将此响应返回给用户。
到目前为止,我所拥有的是 HandlerInterceptor 的实现。这个拦截器有效,但是,我对通过在 postHandle 方法中抛出异常来避免 afterCompletion 的方式并不满意。 如果我没有抛出错误,默认错误页面会附加到 afterCompletion 步骤中的正确响应中。
这是我目前的代码:
public class RequestProcessingInterceptor implements HandlerInterceptor {
private final Logger log = LoggerFactory.getLogger(RequestProcessingInterceptor.class);
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
log.info("Doing some security stuff now ...");
log.warn("... security ok ... since I am not really checking stuff");
return true;
}
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
log.info("Forwarding request and sending that info back ...");
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(UriBuilder.fromUri("http://localhost:8080").build());
response.setContentType("application/json");
response.getWriter().write(service.path(modelAndView.getModel().get("path").toString()).accept("application/json").get(String.class));
response.setStatus(200);
throw new Exception("Need to avoid the execution of the afterCompletion. Only way to do so is by throwing an exception...");
}
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
}
}
是否有更合适的方法来干预 Spring 生命周期或获取上述行为?
【问题讨论】:
-
我之前没有亲自使用过,但我很确定 Spring Security 就是你要找的东西:projects.spring.io/spring-security
标签: spring rest interceptor facade