用户登录时,将用户信息放到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
51
52
53
54
55
56
57
|
package cn.woniubushiniu.controller; import cn.woniubushiniu.po.user; import org.springframework.stereotype.controller; import org.springframework.ui.model; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import javax.servlet.http.httpsession; @controller public class usercontroller { /** * 向用户登录页面跳转 */ @requestmapping (value = "/login" ,method = requestmethod.get) public string tologin(){ return "login" ; } /** * 用户登录 * @param user * @param model * @param session * @return */ @requestmapping (value = "/login" ,method = requestmethod.post) public string login(user user, model model, httpsession session){ //获取用户名和密码 string username=user.getusername(); string password=user.getpassword(); //些处横板从数据库中获取对用户名和密码后进行判断 if (username!= null &&username.equals( "admin" )&&password!= null &&password.equals( "admin" )){ //将用户对象添加到session中 session.setattribute( "user_session" ,user); //重定向到主页面的跳转方法 return "redirect:main" ; } model.addattribute( "msg" , "用户名或密码错误,请重新登录!" ); return "login" ; } @requestmapping (value = "/main" ) public string tomain(){ return "main" ; } @requestmapping (value = "/logout" ) public string logout(httpsession session){ //清除session session.invalidate(); //重定向到登录页面的跳转方法 return "redirect:login" ; } } |
拦截未登录的用户
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
|
public class logininterceptor implements handlerinterceptor { @override public boolean prehandle(httpservletrequest request, httpservletresponse response, object o) throws exception { //获取请求的rui:去除http:localhost:8080这部分剩下的 string uri = request.getrequesturi(); //utl:除了login.jsp是可以公开访问的,其他的url都进行拦截控制 if (uri.indexof( "/login" ) >= 0 ) { return true ; } //获取session httpsession session = request.getsession(); user user = (user) session.getattribute( "user_session" ); //判断session中是否有用户数据,如果有,则返回true,继续向下执行 if (user != null ) { return true ; } //不符合条件的给出提示信息,并转发到登录页面 request.setattribute( "msg" , "您还没有登录,请先登录!" ); request.getrequestdispatcher( "/web-inf/jsp/login.jsp" ).forward(request, response); return false ; } @override public void posthandle(httpservletrequest httpservletrequest, httpservletresponse httpservletresponse, object o, modelandview modelandview) throws exception { } @override public void aftercompletion(httpservletrequest httpservletrequest, httpservletresponse httpservletresponse, object o, exception e) throws exception { } } |
配置文件
配置到spring 的配置文件中
1
2
3
4
5
6
|
<!--登录拦截器--> <mvc:interceptor> <mvc:mapping path= "/**" /> <bean class = "cn.woniubushiniu.interceptor.logininterceptor" /> </mvc:interceptor> </mvc:interceptors> |
配置web.xml 拦截所有url,并设置需要扫描的spring文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<!--配置前端控制器--> <servlet> <servlet-name>springmvc</servlet-name> <servlet- class >org.springframework.web.servlet.dispatcherservlet</servlet- class > <init-param> <param-name>contextconfiglocation</param-name> <param-value>classpath:springmvc-config.xml</param-value> </init-param> <load-on-startup> 1 </load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://segmentfault.com/a/1190000015642264