实现需求:
1.用户未登录,跳转到登录页,登录完成后会跳到初始访问页。
2.用户自定义处理(如需要激活),跳转到激活页面,激活完成后会跳到初始访问页。
使用到的框架
springmvc 的拦截器
实现:
1.编写拦截器通过session保存初始访问的页面地址,便于后面回跳这个页面做准备。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.web.servlet.handlerinterceptor; import org.springframework.web.servlet.modelandview; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import javax.servlet.http.httpsession; /** * 用户登录以后跳转回之前页面的拦截器 拦截对象: 除登录,注册之外的所有跳转页面的请求 因为用户随时可能进行登录操作 * * @version 1.0.0 * @date 2018 -10-19 */ public class forwardbeforeurlinteceptor implements handlerinterceptor { private logger logger = loggerfactory.getlogger( this .getclass()); @override public void aftercompletion(httpservletrequest arg0, httpservletresponse arg1, object arg2, exception arg3) throws exception { } @override public void posthandle(httpservletrequest arg0, httpservletresponse arg1, object arg2, modelandview arg3) throws exception { } @override public boolean prehandle(httpservletrequest request, httpservletresponse response, object arg2) throws exception { // 过滤掉ajax请求 if (request.getheader( "x-requested-with" ) != null && request.getheader( "x-requested-with" ).equalsignorecase( "xmlhttprequest" )) { return true ; } // 获取当前会话 httpsession session = request.getsession( true ); // 拿到上一个页面地址 string uri = request.getrequesturi(); // 去掉项目地址长度的字符(因为我的默认项目地址是给出的) string path = uri.substring(request.getcontextpath().length()); // 得到参数 string query = request.getquerystring(); if (query == null ) { query = "" ; } else { query = "?" + query; } string beforepath = path + query; session.setattribute( "beforepath" , beforepath); session.setattribute( "method" , request.getmethod()); logger.debug( "beforepath :{}, method:{}" , beforepath, request.getmethod()); return true ; } } |
2.在spring的xml配置文件中配置拦截器,例如application.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<mvc:interceptors> <!-- 使用bean定义一个interceptor,直接定义在mvc:interceptors根下面的interceptor将拦截所有的请求 --> <!-- 配置用于跳回登录之前的页面的拦截器--> <mvc:interceptor> <!-- 进行拦截:/**表示拦截所有url及其子路径 --> <mvc:mapping path= "/**" /> <!-- ajax请求的action不进行拦截 --> <mvc:exclude-mapping path= "/*.ajax" /> <mvc:exclude-mapping path= "/resources/**" /> <mvc:exclude-mapping path= "/activation" /> <bean class = "com.xxx.xxx.xxx.interceptor.forwardbeforeurlinteceptor" /> </mvc:interceptor> </mvc:interceptors> |
注意:<mvc:exclude-mapping path="/activation" />
此处是界面可以直接进入激活页面,此处是排除拦截激活页面,防止页面出现不停的回跳到自己页面。
3.自定义过滤器。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
import javax.servlet.servletrequest; import javax.servlet.servletresponse; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import java.io.ioexception; /** * 课程拦截器,当会员过期或未激活时自动跳转到激活页面 * * @version 1.0.0 * @date 2018 -10-19 */ public class memberfilter extends com.bwjf.framework.shiro.filter.userfilter { @override protected boolean isaccessallowed(servletrequest request, servletresponse response, object mappedvalue) { httpservletresponse httpservletresponse = (httpservletresponse) response; httpservletrequest httpservletrequest = (httpservletrequest) request; if (httpservletrequest.getrequesturi().indexof( "activation" ) > 0 ) { return true ; } myshirouser myshirouser = myuserutil.getcurrentshirouser(); if (!checkemptyutil.isempty(myshirouser) && checkemptyutil.isempty(myshirouser.getactivedate())) { try { // 浏览器跳转到激活页面 httpservletresponse.sendredirect(httpservletrequest.getcontextpath() + "/activation" ); } catch (ioexception e) { e.printstacktrace(); } } return true ; } } |
4.shiro.xml配置自定义过滤器
5.controller激活处理后跳转到初始页面
总结
以上所述是小编给大家介绍的springmvc+shiro自定义过滤器的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:https://blog.csdn.net/hu582205/article/details/83501402