package com.lin.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lin.domain.User;
import com.lin.realm.ShiroDbRealm;
import com.lin.service.UserService;
import com.lin.utils.CipherUtil;
@Controller
public class UserControler {
private static Logger logger = LoggerFactory.getLogger(ShiroDbRealm.class);
@Autowired
private UserService userService;
/**
* 验证springmvc与batis连接成功
* @param id
* @param request
* @return
*/
@RequestMapping("/{id}/showUser")
public String showUser(@PathVariable int id, HttpServletRequest request) {
User user = userService.getUserById(id);
System.out.println(user.getName());
request.setAttribute("user", user);
return "showUser";
}
/**
* 初始登陆界面
* @param request
* @return
*/
@RequestMapping("/login.do")
public String tologin(HttpServletRequest request, HttpServletResponse response, Model model){
logger.debug("来自IP[" + request.getRemoteHost() + "]的访问");
return "login";
}
/**
* 验证用户名和密码
* @param request
* @return
*/
@RequestMapping("/checkLogin.do")
public String login(HttpServletRequest request) {
String result = "login.do";
// 取得用户名
String username = request.getParameter("username");
//取得 密码,并用MD5加密
String password = CipherUtil.generatePassword(request.getParameter("password"));
//String password = request.getParameter("password");
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
Subject currentUser = SecurityUtils.getSubject();
try {
System.out.println("----------------------------");
if (!currentUser.isAuthenticated()){//使用shiro来验证
token.setRememberMe(true);
currentUser.login(token);//验证角色和权限
}
System.out.println("result: " + result);
result = "index";//验证成功
} catch (Exception e) {
logger.error(e.getMessage());
result = "login。do";//验证失败
}
return result;
}
/**
* 退出
* @return
*/
@RequestMapping(value = "/logout")
@ResponseBody
public String logout() {
Subject currentUser = SecurityUtils.getSubject();
String result = "logout";
currentUser.logout();
return result;
}
/**
* 妫?煡
* @return
*/
@RequestMapping(value = "/chklogin", method = RequestMethod.POST)
@ResponseBody
public String chkLogin() {
Subject currentUser = SecurityUtils.getSubject();
if (!currentUser.isAuthenticated()) {
return "false";
}
return "true";
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Shiro登陆验证实例,采用SSM
共60个文件
xml:14个
java:12个
class:12个
5星 · 超过95%的资源 需积分: 10 2.0k 下载量 160 浏览量
2015-12-06
17:12:39
上传
评论 17
收藏 43KB RAR 举报
温馨提示
采用了Spring+SpringMVC+Mybatis+Shiro+Msql来写了一个登陆验证的实例,具体效果和过程看http://blog.csdn.net/evankaka/article/details/50196003
资源推荐
资源详情
资源评论
收起资源包目录
ShiroLearn1 Maven Webapp.rar (60个子文件)
ShiroLearn1 Maven Webapp
.project 1KB
pom.xml 5KB
target
test-classes
com
lin
baseTest
SpringTestCase.class 840B
service
UserServiceTest.class 1KB
m2e-jee
web-resources
META-INF
maven
com.lin
ShiroLearn1
pom.xml 5KB
pom.properties 281B
MANIFEST.MF 104B
classes
log4j.properties 810B
shiro
spring-shiro.xml 2KB
com
lin
serviceImpl
UserServiceImpl.class 1KB
utils
EncryptUtils.class 670B
CipherUtil.class 3KB
dao
UserDao.class 446B
domain
User.class 2KB
UserRole.class 902B
Role.class 1KB
controller
UserControler.class 4KB
mapper
UserMapper.xml 3KB
realm
ShiroDbRealm.class 4KB
service
UserService.class 242B
application.xml 3KB
properties
jdbc.properties 138B
mybatis
mybatis-config.xml 209B
springmvc
spring-mvc.xml 1KB
.settings
.jsdtscope 530B
org.eclipse.wst.common.component 702B
org.eclipse.jdt.core.prefs 664B
org.eclipse.wst.common.project.facet.core.xml 296B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.m2e.core.prefs 90B
org.eclipse.wst.jsdt.ui.superType.name 6B
src
test
resources
java
com
lin
baseTest
SpringTestCase.java 767B
service
UserServiceTest.java 702B
main
resources
log4j.properties 810B
shiro
spring-shiro.xml 2KB
application.xml 3KB
properties
jdbc.properties 138B
mybatis
mybatis-config.xml 209B
springmvc
spring-mvc.xml 1KB
java
com
lin
serviceImpl
UserServiceImpl.java 614B
utils
CipherUtil.java 2KB
EncryptUtils.java 278B
dao
UserDao.java 386B
domain
UserRole.java 572B
Role.java 592B
User.java 1KB
controller
UserControler.java 3KB
mapper
UserMapper.xml 3KB
realm
ShiroDbRealm.java 3KB
service
UserService.java 183B
webapp
WEB-INF
views
login.jsp 766B
index.jsp 3KB
NewFile.jsp 516B
error
noperms.jsp 648B
401.jsp 857B
404.jsp 643B
showUser.jsp 384B
web.xml 3KB
include
common.jsp 294B
.classpath 1KB
共 60 条
- 1
Evankaka
- 粉丝: 7701
- 资源: 129
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页