package com.st.shiro.controller;
import java.util.UUID;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
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.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.st.shiro.util.EncryptionUtil;
import io.swagger.annotations.ApiOperation;
@RestController
public class LoginController {
private static final Logger logger = LoggerFactory.getLogger(LoginController.class);
@Autowired
private EncryptionUtil encryptionUtil;
/*@ApiOperation("登录")
@RequestMapping(value="/login",method=RequestMethod.GET)
public String loginForm(Model model){
return "login";
}*/
/* @ApiOperation("登录")
@RequestMapping(value="/login",method=RequestMethod.GET)
public String login(@RequestParam String userName,@RequestParam String password,RedirectAttributes redirectAttributes){
UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
//获取当前的Subject
Subject currentUser = SecurityUtils.getSubject();
try {
//在调用了login方法后,SecurityManager会收到AuthenticationToken,并将其发送给已配置的Realm执行必须的认证检查
//每个Realm都能在必要时对提交的AuthenticationTokens作出反应
//所以这一步在调用login(token)方法时,它会走到MyRealm.doGetAuthenticationInfo()方法中,具体验证方式详见此方法
logger.info("对用户[" + userName + "]进行登录验证..验证开始");
currentUser.login(token);
logger.info("对用户[" + userName + "]进行登录验证..验证通过");
}catch(UnknownAccountException uae){
logger.info("对用户[" + userName + "]进行登录验证..验证未通过,未知账户");
redirectAttributes.addFlashAttribute("message", "未知账户");
}catch(IncorrectCredentialsException ice){
logger.info("对用户[" + userName + "]进行登录验证..验证未通过,错误的凭证");
redirectAttributes.addFlashAttribute("message", "密码不正确");
}catch(LockedAccountException lae){
logger.info("对用户[" + userName + "]进行登录验证..验证未通过,账户已锁定");
redirectAttributes.addFlashAttribute("message", "账户已锁定");
}catch(ExcessiveAttemptsException eae){
logger.info("对用户[" + userName + "]进行登录验证..验证未通过,错误次数过多");
redirectAttributes.addFlashAttribute("message", "用户名或密码错误次数过多");
}catch(AuthenticationException ae){
//通过处理Shiro的运行时AuthenticationException就可以控制用户登录失败或密码错误时的情景
logger.info("对用户[" + userName + "]进行登录验证..验证未通过,堆栈轨迹如下");
ae.printStackTrace();
redirectAttributes.addFlashAttribute("message", "用户名或密码不正确");
}
//验证是否登录成功
if(currentUser.isAuthenticated()){
logger.info("用户[" + userName + "]登录认证通过(这里可以进行一些认证通过后的一些系统参数初始化操作)");
return "redirect:/index";
}else{
token.clear();
return "redirect:/login";
}
}
*/
@ApiOperation("登录")
@RequestMapping(value="/login",method=RequestMethod.GET)
public String login(@RequestParam String userName,@RequestParam String password,RedirectAttributes redirectAttributes){
UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
//获取当前的Subject
Subject currentUser = SecurityUtils.getSubject();
try {
//在调用了login方法后,SecurityManager会收到AuthenticationToken,并将其发送给已配置的Realm执行必须的认证检查
//每个Realm都能在必要时对提交的AuthenticationTokens作出反应
//所以这一步在调用login(token)方法时,它会走到MyRealm.doGetAuthenticationInfo()方法中,具体验证方式详见此方法
logger.info("对用户[" + userName + "]进行登录验证..验证开始");
currentUser.login(token);
logger.info("对用户[" + userName + "]进行登录验证..验证通过");
}catch(UnknownAccountException uae){
logger.info("对用户[" + userName + "]进行登录验证..验证未通过,未知账户");
redirectAttributes.addFlashAttribute("message", "未知账户");
}catch(IncorrectCredentialsException ice){
logger.info("对用户[" + userName + "]进行登录验证..验证未通过,错误的凭证");
redirectAttributes.addFlashAttribute("message", "密码不正确");
}catch(LockedAccountException lae){
logger.info("对用户[" + userName + "]进行登录验证..验证未通过,账户已锁定");
redirectAttributes.addFlashAttribute("message", "账户已锁定");
}catch(ExcessiveAttemptsException eae){
logger.info("对用户[" + userName + "]进行登录验证..验证未通过,错误次数过多");
redirectAttributes.addFlashAttribute("message", "用户名或密码错误次数过多");
}catch(AuthenticationException ae){
//通过处理Shiro的运行时AuthenticationException就可以控制用户登录失败或密码错误时的情景
logger.info("对用户[" + userName + "]进行登录验证..验证未通过,堆栈轨迹如下");
ae.printStackTrace();
redirectAttributes.addFlashAttribute("message", "用户名或密码不正确");
}
//验证是否登录成功
if(currentUser.isAuthenticated()){
logger.info("用户[" + userName + "]登录认证通过(这里可以进行一些认证通过后的一些系统参数初始化操作)");
return "redirect:/index";
}else{
token.clear();
return "redirect:/login";
}
}
/**
* 退出登录
* @param redirectAttributes
* @return
*/
@RequestMapping(value="/logout",method=RequestMethod.GET)
public String logout(RedirectAttributes redirectAttributes ){
//使用权限管理工具进行用户的退出,跳出登录,给出提示信息
SecurityUtils.getSubject().logout();
redirectAttributes.addFlashAttribute("message", "您已安全退出");
return "redirect:/login";
}
/**
* 没有权限
* @return
*/
@RequestMapping(value="/403",method=RequestMethod.POST)
public String unauthorizedRole(){
logger.info("------没有权限-------");
return "403";
}
@ApiOperation("注册")
@RequestMapping(value="/regist",method=RequestMethod.GET)
public void regist(){
String uuid=UUID.randomUUID().toString();
String pass
没有合适的资源?快使用搜索试试~ 我知道了~
springboot整合shiro,redis缓存session
共71个文件
class:19个
java:19个
yml:10个
1星 需积分: 50 84 下载量 138 浏览量
2018-02-09
15:19:53
上传
评论 1
收藏 2.34MB RAR 举报
温馨提示
为实现Web应用的分布式集群部署,要解决登录session的统一。本文利用shiro做权限控制,redis做session存储,结合spring boot快速配置实现session共享。
资源推荐
资源详情
资源评论
收起资源包目录
st-shiro.rar (71个子文件)
st-shiro
src
main
resources
application-dev.yml 4KB
ehcache-shiro.xml 379B
index.jsp 351B
application-sit.yml 3KB
application-pord.yml 3KB
application.yml 42B
application-uat.yml 3KB
java
com
st
start
StShiroApplication.java 1KB
shiro
dto
SysUser.java 4KB
CommentCodeVO.java 359B
filter
LoginInterceptor.java 3KB
dao
RedisSessionDAO.java 2KB
realm
MyShiroRealm.java 4KB
controller
LoginController.java 8KB
util
EncryptionUtil.java 555B
UserUtil.java 860B
cache
ShiroCache.java 2KB
RedisCacheManager.java 717B
constant
RedisConstant.java 536B
Constant.java 401B
service
impl
STShiroServiceImpl.java 3KB
STShiroService.java 805B
mapper
xml
STShiroMapper.xml 826B
STShiroMapper.java 825B
conf
STShiroConf.java 2KB
MyWebAppConfigurer.java 1KB
ShiroConfiguration.java 5KB
test
resources
java
.classpath 1KB
.settings
org.eclipse.m2e.core.prefs 90B
org.eclipse.core.resources.prefs 191B
org.eclipse.jdt.core.prefs 243B
pom.xml 691B
target
classes
META-INF
MANIFEST.MF 324B
maven
com.st
st-shiro
pom.properties 210B
pom.xml 691B
com
st
start
StShiroApplication.class 1KB
shiro
dto
SysUser.class 5KB
CommentCodeVO.class 724B
filter
LoginInterceptor.class 4KB
dao
RedisSessionDAO.class 3KB
realm
MyShiroRealm.class 5KB
controller
LoginController.class 5KB
util
UserUtil.class 1KB
EncryptionUtil.class 997B
cache
RedisCacheManager.class 1KB
ShiroCache.class 4KB
constant
RedisConstant.class 590B
Constant.class 484B
service
impl
STShiroServiceImpl.class 4KB
STShiroService.class 704B
mapper
STShiroMapper.class 636B
xml
STShiroMapper.xml 826B
conf
STShiroConf.class 3KB
MyWebAppConfigurer.class 2KB
ShiroConfiguration.class 6KB
application-dev.yml 4KB
ehcache-shiro.xml 379B
index.jsp 351B
application-sit.yml 3KB
application-pord.yml 3KB
application.yml 42B
application-uat.yml 3KB
test-classes
.project 560B
logs
st-base.log.5 10.05MB
st-base.log.2 10.04MB
st-base.log.6 10.02MB
st-base.log 3.25MB
st-base.log.4 10.15MB
st-base.log.1 10.02MB
st-base.log.3 10MB
st-base.log.7 10.15MB
共 71 条
- 1
资源评论
- jared_he20172018-04-30什么垃圾,有好多自定义的类都没有,根本不能运行
- budadota22018-04-12pom文件不正确,工程不恩正常运行
qq_16055765
- 粉丝: 81
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功