使用session和cookie实现免登陆
来源:互联网 发布:动物大百科软件下载 编辑:程序博客网 时间:2024/05/17 03:48
原文链接:http://www.jb51.net/article/108764.htm
参考文件:http://blog.csdn.net/a997208868/article/details/47787305
后台管理页面往往需要登录才可以进行操作,这时就需要Seession来记录登录状态
要实现起来也是非常简单,只需要自定义一个HandlerInterceptor就行了
自定义的HandlerInterceptor也只有短短几行代码
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
public
class
LoginInterceptor
implements
HandlerInterceptor {
@Override
public
void
afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object obj, Exception err)
throws
Exception {
}
@Override
public
void
postHandle(HttpServletRequest request, HttpServletResponse response,
Object obj, ModelAndView mav)
throws
Exception {
}
@Override
public
boolean
preHandle(HttpServletRequest request, HttpServletResponse response,
Object obj)
throws
Exception {
//获取session里的登录状态值
String str = (String) request.getSession().getAttribute(
"isLogin"
);
//如果登录状态不为空则返回true,返回true则会执行相应controller的方法
if
(str!=
null
){
return
true
;
}
//如果登录状态为空则重定向到登录页面,并返回false,不执行原来controller的方法
response.sendRedirect(
"/backend/loginPage"
);
return
false
;
}
}
Controller代码
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
@Controller
@RequestMapping
(
"/backend"
)
public
class
BackendController {
@RequestMapping
(value =
"/loginPage"
, method = {RequestMethod.GET})
public
String loginPage(HttpServletRequest request,String account, String password){
return
"login"
;
}
@RequestMapping
(value =
"/login"
, method = {RequestMethod.POST})
public
String login(HttpServletRequest request,RedirectAttributes model, String account, String password){
//验证账号密码,如果符合则改变session里的状态,并重定向到主页
if
(
"jack"
.equals(account)&&
"jack2017"
.equals(password)){
request.getSession().setAttribute(
"isLogin"
,
"yes"
);
return
"redirect:IndexPage"
;
}
else
{
//密码错误则重定向回登录页,并返回错误,因为是重定向所要要用到RedirectAttributes
model.addFlashAttribute(
"error"
,
"密码错误"
);
return
"redirect:loginPage"
;
}
}
//登出,移除登录状态并重定向的登录页
@RequestMapping
(value =
"/loginOut"
, method = {RequestMethod.GET})
public
String loginOut(HttpServletRequest request) {
request.getSession().removeAttribute(
"isLogin"
);
return
"redirect:loginPage"
;
}
@RequestMapping
(value =
"/IndexPage"
, method = {RequestMethod.GET})
public
String IndexPage(HttpServletRequest request){
return
"Index"
;
}
}
spring的配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!--省略其他基本配置-->
<!-- 配置拦截器 -->
<
mvc:interceptors
>
<!-- 配置登陆拦截器 -->
<
mvc:interceptor
>
<!--拦截后台页面的请求-->
<
mvc:mapping
path
=
"/backend/**"
/>
<!--不拦截登录页和登录的请求-->
<
mvc:exclude-mapping
path
=
"/backend/loginPage"
/>
<
mvc:exclude-mapping
path
=
"/backend/login"
/>
<
bean
class
=
"com.ima.Interceptor.LoginInterceptor"
></
bean
>
</
mvc:interceptor
>
</
mvc:interceptors
>
一个简单的Session实现登录认证系统就这样完成了,如果想登录状态退出浏览器后仍保留一段时间的可以将Session改为Cookie
一般情况下我们都会使用Cookie
Cookie和Session的方法差不多
使用Cookie的自定义HandlerInterceptor
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
public
class
LoginInterceptor
implements
HandlerInterceptor {
@Override
public
void
afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object obj, Exception err)
throws
Exception {
}
@Override
public
void
postHandle(HttpServletRequest request, HttpServletResponse response,
Object obj, ModelAndView mav)
throws
Exception {
}
@Override
public
boolean
preHandle(HttpServletRequest request, HttpServletResponse response,
Object obj)
throws
Exception {
// 获取request的cookie
Cookie[] cookies = request.getCookies();
if
(
null
==cookies) {
System.out.println(
"没有cookie=============="
);
}
else
{
// 遍历cookie如果找到登录状态则返回true执行原来controller的方法
for
(Cookie cookie : cookies){
if
(cookie.getName().equals(
"isLogin"
)){
return
true
;
}
}
}
// 没有找到登录状态则重定向到登录页,返回false,不执行原来controller的方法
response.sendRedirect(
"/backend/loginPage"
);
return
false
;
}
}
Controller的变化也不大
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
@Controller
@RequestMapping
(
"/backend"
)
public
class
BackendController {
@RequestMapping
(value =
"/loginPage"
, method = {RequestMethod.GET})
public
String loginPage(HttpServletRequest request, String account, String password) {
return
"login"
;
}
@RequestMapping
(value =
"/login"
, method = {RequestMethod.POST})
public
String login(HttpServletRequest request, HttpServletResponse response, RedirectAttributes model, String account, String password) {
if
(
"edehou"
.equals(account) &&
"aidou2017"
.equals(password)) {
Cookie cookie =
new
Cookie(
"isLogin"
,
"yes"
);
cookie.setMaxAge(
30
*
60
);
// 设置为30min
cookie.setPath(
"/"
);
response.addCookie(cookie);
return
"redirect:IndexPage"
;
}
else
{
model.addFlashAttribute(
"error"
,
"密码错误"
);
return
"redirect:loginPage"
;
}
}
@RequestMapping
(value =
"/logOut"
, method = {RequestMethod.GET})
public
String loginOut(HttpServletRequest request, HttpServletResponse response) {
Cookie[] cookies = request.getCookies();
for
(Cookie cookie : cookies) {
if
(cookie.getName().equals(
"isLogin"
)) {
cookie.setValue(
null
);
cookie.setMaxAge(
0
);
// 立即销毁cookie
cookie.setPath(
"/"
);
response.addCookie(cookie);
break
;
}
}
return
"redirect:loginPage"
;
}
@RequestMapping
(value =
"/IndexPage"
, method = {RequestMethod.GET})
public
String IndexPage(HttpServletRequest request) {
return
"Index"
;
}
}
0 0
- 使用session和cookie实现免登陆
- session、cookie实现免登录
- 使用Spring AOP和Cookie做网站免登陆
- 使用session实现网站N天免登陆
- 使用session实现网站N天免登陆(转载)
- Session和Cookie小结以及PHP单点登陆的实现
- 利用cookie, session 和Filter实现简单的自动登陆
- C#.net cookie、session实现自动登陆
- python利用selenium获取cookie实现免登陆
- 浅谈利用Cookie技术实现3天免登陆
- cookie应用三天免登陆
- 使用php+session实现登陆
- passport登陆的实现--跨域共享cookie和跨域共享session
- 使用HttpClient模拟网站登陆sina邮箱(保持远程服务器session和cookie)
- 引用 使用Session和Cookie实现网站自动登录
- ASP.Net之使用Cookie和Session实现自动登录
- SESSION和COOKIE的使用
- Rails Cookie和session使用
- Gradle 1.12用户指南翻译——第六十一章. Gradle包装器
- Next Greater Element I问题及解法
- android基础知识----自带的分享
- Linux上RabbitMQ安装
- PCA算法的各种实现
- 使用session和cookie实现免登陆
- Codeforces Round #408 (Div. 2)-D. Police Stations-多起点bfs
- SunlightDB 安全策略(一)
- 第11章、贡献,Bug报告,得到帮助
- cordova -d platform add android失败,报错 registy.rpm.taobao.org 失败
- 网络直播“涉黄”屡禁不止,强化监管势在必行
- bzoj4822[CQOI2017]:老C的任务(树状数组/k-dtree)
- 分类算法之朴素贝叶斯分类(Naive Bayesian classification)
- LVS基本配置