在学spring3 mvc,做了个简单的CRUD,但是用户不登录也能直接访问任何页面。我的想法是写个SecurityInterceptor在preHandle中判断session是不是存在user对象。
配置如下:
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/*" /> <bean class="smartcrud.common.spring.SecurityInterceptor"> </bean> </mvc:interceptor> </mvc:interceptors>
public class SecurityInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // intercept HttpSession session = request.getSession(); if (session.getAttribute("user") == null) { throw new AuthorizationException(); } else { return true; } }
于是修改配置如下:
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/*" /> <bean class="smartcrud.common.spring.SecurityInterceptor"> <property name="excludedUrls"> <list> <value>/login</value> </list> </property> </bean> </mvc:interceptor> </mvc:interceptors>
public class SecurityInterceptor implements HandlerInterceptor { private List<String> excludedUrls; public void setExcludedUrls(List<String> excludedUrls) { this.excludedUrls = excludedUrls; } @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // excluded URLs: // see http://stackoverflow.com/questions/9908124/spring-mvc-3-interceptor-on-all-excluding-some-defined-paths String requestUri = request.getRequestURI(); for (String url : excludedUrls) { if (requestUri.endsWith(url)) { return true; } } // intercept HttpSession session = request.getSession(); if (session.getAttribute("user") == null) { // see http://stackoverflow.com/questions/12713873/spring-3-1-how-do-you-send-all-exception-to-one-page throw new AuthorizationException(); } else { return true; } }
这样以/login结尾的请求不做拦截处理。。
接下来需要处理非/login结尾的情况,此时我设计为抛出一个自定义的AuthorizationException异常。
public class AuthorizationException extends Exception { }
当抛出这个异常时,spring框架应该能够处理它,并将用户导向/WEB-INF/views/adminLogin.jsp页面以便让用户登录。
搜索了一下资料,配置如下:
<bean id="handlerExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="smartcrud.common.exception.AuthorizationException">redirect:/login</prop> </props> </property> </bean>
代码如下:
@Controller public class LoginController { @Autowired private UserService userService; @RequestMapping(value = "/login", method = RequestMethod.GET) public String loginForm() { return "adminLogin"; }
<bean id="handlerExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="smartcrud.common.exception.AuthorizationException">/login</prop> </props> </property> </bean>
以上为个人转载,不作为博主个人观点,仅供参考。
另感谢原文章博主
该文章转载自http://www.itniwo.net/blog/v/254207.html