Java MVC拦截器vs Spring安全过滤器vs其他东西。。。?
我正在为我的web应用程序使用SpringMVC和SpringSecurity。它包括用户注册页面和专用用户面板。目前,我已使用以下URL模式对其进行了设置:Java MVC拦截器vs Spring安全过滤器vs其他东西。。。?,java,spring-mvc,filter,spring-security,interceptor,Java,Spring Mvc,Filter,Spring Security,Interceptor,我正在为我的web应用程序使用SpringMVC和SpringSecurity。它包括用户注册页面和专用用户面板。目前,我已使用以下URL模式对其进行了设置: whatever/myapp/login用户登录 whatever/myapp/register?步骤=1开始注册 whatever/myapp/account/**私人区域视图(页面) whatever/myapp/pending注册后流程完成时显示的视图 whatever/myapp/blockedaccount blocked v
用户登录whatever/myapp/login
开始注册whatever/myapp/register?步骤=1
私人区域视图(页面)whatever/myapp/account/**
注册后流程完成时显示的视图whatever/myapp/pending
account blocked viewwhatever/myapp/blocked
如果注册失败,允许重试whatever/myapp/register/retry
(私人区域页面)无论什么/myapp/account/**
(此页面设置了一个计时器以重定向到/account/home)whatever/myapp/pending
whatever/myapp/register/retry
/account/**
),则应根据状态向其显示相应的视图(重定向到相应的页面)。我已定义这些状态:
-与挂起视图相关挂起
-允许完全访问已启用
-此处不相关已禁用
-与重试视图相关重试\u允许
-与帐户阻止视图相关blocked
/account/**
,它检查用户状态,并重定向到适当的页面,但不知何故,我感觉这并不是理想的或适当的解决方案,因为我面临着奇怪的行为,比如多控制器调用。。。而且我也不太确定在preHandle()
方法中何时返回true
/false
。以下是来自拦截器的代码片段:
@Override
public boolean preHandle(
HttpServletRequest request,
HttpServletResponse response,
Object arg2)
throws Exception {
IPanelUser pUser = (IPanelUser) SecurityContextHolder.getContext()
.getAuthentication().getPrincipal();
// check principal first and then load from DB
// "suspended" is initial status upon registration
if(pUser.getCustomer().getStatus() == CustomerStatus.Suspended.getCode()) {
// if suspended, load from DB and update status
Customer customer = this.customerService.getUserByUsername(pUser.getUsername());
if(customer != null)
pUser.getCustomer().setStatus(customer.getStatus());
// still suspended? redirect to pending
if(pUser.getCustomer().getStatus() == CustomerStatus.Suspended.getCode()) {
response.sendRedirect("../pending");
return false;
}
}
if(pUser.getCustomer().getStatus() == CustomerStatus.Blocked.getCode()) {
// redirect to blocked page
response.sendRedirect("../blocked");
SecurityContextHolder.clearContext();
return false;
}
if(pUser.getCustomer().getStatus() == CustomerStatus.AllowRetry.getCode()) {
// redirect to CC submission page
response.sendRedirect("../register/retry");
return false;
}
if(pUser.getCustomer().getStatus() == CustomerStatus.Enabled.getCode() ||
pUser.getCustomer().getStatus() == CustomerStatus.Disabled.getCode()) {
// do nothing
}
return true;
}
这是一种有效的方法吗?是否有其他建议?所有选项都有效,这取决于您想要的抽象级别 在
过滤器
中,您只能访问HttpServletRequest
和HttpServletResponse
对象,因此您与Servlet
API紧密结合。您也不能(直接)访问所有优秀的Spring功能,例如返回要渲染的视图或响应属性
在HandlerInterceptor
中,它又是相同的。您可以直接在preHandle()
中执行重定向或请求处理,在那里您无权访问ModelAndView
或设置一个您签入postHandle()
的标志。您可以访问模型和视图
,但不能访问其他一些Spring MVC功能
SpringSecurity是一个不错的选择,但我发现它有很多我不太喜欢的配置
我最喜欢的最后一种选择是使用AOP(您也可以使用SpringSecurity或Shiro)。创建一个类似于@Private
的注释,并注释@Controller
处理程序方法。您可以使用AOP来建议这些方法。该建议基本上是检查某个会话或请求属性的标志(已授权或未授权)。如果允许,则继续执行处理程序方法,如果不允许,则抛出UnauthorizedException
(或类似)。然后,您还为该异常声明了一个异常,在该异常中,您几乎可以完全控制响应的生成方式:aModelAndView
(及相关)、aResponseEntity
、使用@ResponseBody
注释处理程序、直接编写响应等等。如果您需要,我觉得您有更多的控制权