<!--控制器异常处理 --> <bean id="exceptionHandlerExceptionResolver" class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver"> </bean> <bean class="com.sohu.pps.realm.exception.MyExceptionHander"/>不知道咋回事,我注视掉这些配置文件后,自己写的错误异常控制器还是能用,不知道哪位大神可以解答下。
@ControllerAdvice public class MyExceptionHander { /** * 没有权限 异常 * 后续根据不同的需求定制即可 */ @ExceptionHandler({UnauthorizedException.class}) @ResponseStatus(HttpStatus.UNAUTHORIZED) public ModelAndView processUnauthenticatedException(NativeWebRequest request, UnauthorizedException e) { ModelAndView mv = new ModelAndView(); mv.addObject("exception", e); mv.setViewName("unauthorized"); return mv; } /** * 没有验证通过异常 * 后续根据不同的需求定制即可 */ @ExceptionHandler({UnknownAccountException.class}) @ResponseStatus(HttpStatus.NOT_FOUND) public ModelAndView processUnknownAccountException(NativeWebRequest request, UnknownAccountException e) { String error; ModelAndView mv = new ModelAndView(); mv.addObject("exception", e); mv.setViewName("unauthorized"); return mv; }
<bean id="rememberMeCookie" class="org.apache.shiro.web.servlet.SimpleCookie"> <constructor-arg value="rememberMe"/> <property name="httpOnly" value="true"/> <property name="maxAge" value="604800"/><!-- 一个星期 --> </bean> <!-- rememberMe管理器 --> <bean id="rememberMeManager" class="org.apache.shiro.web.mgt.CookieRememberMeManager"> <property name="cipherKey" value="#{T(org.apache.shiro.codec.Base64).decode('4AvVhmFLUs0KTA3Kprsdag==')}"/> <property name="cookie" ref="rememberMeCookie"/> </bean>然后在安全管理器里面加入RememberMe属性:
<property name="rememberMeParam" value="rememberMe"/>
控制器里面就可以使用了:@ResponseBody @RequestMapping(value="/login",method = RequestMethod.POST) public ModelAndView loginAction(@RequestParam("name")String name,@RequestParam("password")String password, @RequestParam(value = "rememberMe",required = false) boolean rememberMe ) { ModelAndView mv=new ModelAndView(); UsernamePasswordToken token=new UsernamePasswordToken(name,password); if(rememberMe) { token.setRememberMe(true); } else { token.setRememberMe(false); } SecurityUtils.getSubject().login(token); mv.setViewName("loginSuccess"); return mv; }
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" keystoreFile="D:\localhost.keystore" keystorePass="123456"/>
<bean id="sslFilter" class="org.apache.shiro.web.filter.authz.SslFilter"> <property name="port" value="8443"/> </bean>这样就可以在shiroFilter里面使用ssl拦截器拦截相应的URL了。
原文地址:http://blog.csdn.net/js_sky/article/details/45598621