package com.shiro.controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.shiro.Utils.EncryptUtils;
import com.shiro.model.User;
import com.shiro.service.ShiroServer;
@Controller
@RequestMapping(value = "login")
public class LoginController{
ShiroServer shiroServer=new ShiroServer();
/*
* @Autowired User user;
*/
/**
* 用户登录
*
* @param user
* 登录用户
* @return
*/
@RequestMapping(params = "main")
public ModelAndView login(User user,HttpSession session, HttpServletRequest request) {
ModelAndView modelView = new ModelAndView();
Subject currentUser = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken(
user.getUsercode(), EncryptUtils.encryptMD5(user.getPassword()));
token.setRememberMe(true);
try {
currentUser.login(token);
modelView.setViewName("/main");
} catch (AuthenticationException e) {
modelView.addObject("message", "login errors");
modelView.setViewName("/login");
e.printStackTrace();
}
return modelView;
}
/**
* 退出登录
*
* @return
*/
@RequestMapping(params = "logout")
public String logout() {
Subject currentUser = SecurityUtils.getSubject();
try {
currentUser.logout();
} catch (AuthenticationException e) {
e.printStackTrace();
}
return "/login";
}
@RequestMapping(params = "myjsp")
public ModelAndView login2() {
System.out.println("sss");
ModelAndView modelView = new ModelAndView();
modelView.addObject("message", "登录成功!");
modelView.setViewName("/my");
return modelView;
}
/**
* 自动登录失败
* 如何获取RememberMe的用户信息
* @return
*/
@RequestMapping(params = "test")
public ModelAndView login3() {
ModelAndView modelView = new ModelAndView();
Subject currentUser = SecurityUtils.getSubject();
if(!currentUser.isAuthenticated() && currentUser.isRemembered()){
Object principal = currentUser.getPrincipal();
if (null != principal) {
User user = shiroServer.findUserByName(String.valueOf(principal));
String password = user.getPassword();
UsernamePasswordToken token = new UsernamePasswordToken(user.getUserName(), EncryptUtils.encryptMD5(password));
token.setRememberMe(true);
currentUser.login(token);//登录
modelView.setViewName("/main");
}
}else{
modelView.setViewName("/login");
}
return modelView;
}
@RequestMapping(params = "save")
public void save(String role,String user,String resources,HttpServletResponse response) throws IOException{
try {
shiroServer.saveUser(role, user, resources);
} catch (Exception e) {
e.printStackTrace();
}
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.print("<script>alert('保存成功');window.location.href='common.jsp';</script>");
out.flush();
out.close();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
shiro权限框架例子 (113个子文件)
LoginController.class 4KB
MonitorRealm.class 4KB
ShiroServer.class 4KB
OnlineFilter.class 3KB
AccessInterceptor.class 2KB
User.class 1KB
UserController.class 1KB
EncryptUtils.class 649B
.classpath 6KB
Thumbs.db 19KB
top_bg.gif 519B
blank.gif 43B
myReam.ini 434B
myReam.ini 130B
hibernate3.jar 3.75MB
aspectjweaver-1.6.8.jar 1.55MB
ojdbc14.jar 1.47MB
jackson-all-1.8.5.jar 989KB
org.springframework.context-3.1.1.RELEASE.jar 812KB
mysql-connector-java-5.1.12-bin.jar 716KB
mysql-connector-5.1.8.jar 707KB
org.springframework.context-3.0.5.RELEASE.jar 653KB
hsqldb.jar 629KB
javassist-3.12.0.GA.jar 618KB
org.springframework.beans-3.1.1.RELEASE.jar 576KB
commons-collections-3.2.1.jar 562KB
org.springframework.web.servlet-3.1.1.RELEASE.jar 560KB
org.springframework.beans-3.0.5.RELEASE.jar 542KB
org.springframework.web-3.1.1.RELEASE.jar 532KB
shiro-all-1.2.1.jar 521KB
log4j-1.2.16.jar 470KB
org.springframework.core-3.1.1.RELEASE.jar 439KB
antlr-2.7.7.jar 435KB
antlr-2.7.6.jar 433KB
org.springframework.web.servlet-3.0.5.RELEASE.jar 409KB
jstl-1.2.jar 405KB
org.springframework.web-3.0.5.RELEASE.jar 386KB
org.springframework.jdbc-3.0.5.RELEASE.jar 377KB
org.springframework.core-3.0.5.RELEASE.jar 373KB
org.springframework.orm-3.0.5.RELEASE.jar 326KB
org.springframework.aop-3.1.1.RELEASE.jar 324KB
org.springframework.aop-3.0.5.RELEASE.jar 314KB
dom4j-1.6.1.jar 307KB
jtds-1.2.4.jar 298KB
jtds-1.2.5.jar 295KB
morph-1.1.1.jar 286KB
hibernate-validator-4.1.0.Final.jar 281KB
cglib-2.2.jar 272KB
commons-lang-2.4.jar 256KB
org.springframework.transaction-3.0.5.RELEASE.jar 226KB
asm-all-3.2.jar 203KB
commons-beanutils.jar 184KB
org.springframework.jms-3.0.5.RELEASE.jar 181KB
servlet-api.jar 173KB
org.springframework.expression-3.1.1.RELEASE.jar 172KB
org.springframework.expression-3.0.5.RELEASE.jar 166KB
commons-io-2.0.1.jar 156KB
json-lib-2.2.2-jdk15.jar 141KB
javax.persistence.jar 131KB
aspectjrt-1.6.8.jar 113KB
commons-io-1.4.0.jar 108KB
org.springframework.context.support-3.1.1.RELEASE.jar 105KB
org.springframework.context.support-3.0.5.RELEASE.jar 99KB
ezmorph-1.0.6.jar 84KB
org.springframework.oxm-3.0.5.RELEASE.jar 60KB
commons-logging.jar 59KB
commons-logging-1.1.1.jar 59KB
org.springframework.asm-3.0.5.RELEASE.jar 52KB
commons-fileupload.jar 52KB
org.springframework.asm-3.1.1.RELEASE.jar 52KB
org.springframework.aspects-3.1.1.RELEASE.jar 49KB
org.springframework.aspects-3.0.5.RELEASE.jar 35KB
slf4j-api-1.6.1.jar 25KB
javax.transaction.jar 23KB
slf4j-api-1.5.2.jar 17KB
slf4j-log4j12-1.6.1.jar 10KB
slf4j-log4j12-1.5.2.jar 9KB
aopalliance-1.0.0.jar 5KB
aopalliance-1.0.jar 4KB
LoginController.java 3KB
MonitorRealm.java 3KB
ShiroServer.java 3KB
OnlineFilter.java 2KB
User.java 958B
AccessInterceptor.java 917B
UserController.java 841B
EncryptUtils.java 280B
login-logo.jpg 35KB
jquery-2.0.0.js 235KB
jquery-2.0.0.min.js 81KB
login.jsp 3KB
common.jsp 2KB
index.jsp 889B
main.jsp 665B
noperms.jsp 655B
my.jsp 498B
notmyjsp.jsp 487B
MANIFEST.MF 36B
.mymetadata 300B
SyProLogo.png 10KB
共 113 条
- 1
- 2
牛儿力气大
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页