java-shiro登录验证
登录验证:
LoginController:(LoginController.java)
@ResponseBody
@RequestMapping(value="/login",method=RequestMethod.POST)
public ResponseResult login(User user, HttpServletRequest request) {
ResponseResult responseResult = new ResponseResult(ResponseResult.FAILURECODE,"登陆失败");
String loginName = user.getLoginName();
String passWord = user.getPassWord();
String eccodePassWord = MD5Operation.getEncryptedPwd(passWord); /*调用shiro判断当前用户是否是系统用户*/
//得到当前用户
Subject subject = SecurityUtils.getSubject();
//判断是否登录,如果未登录,则登录
if (!subject.isAuthenticated()) {
//创建用户名/密码验证Token, shiro是将用户录入的登录名和密码(未加密)封装到uPasswordToken对象中
UsernamePasswordToken uPasswordToken = new UsernamePasswordToken(loginName,eccodePassWord);
//自动调用AuthRealm.doGetAuthenticationInfo
try {
//执行登录,如果登录未成功,则捕获相应的异常
subject.login(uPasswordToken);
responseResult.setMsg("登录成功");
responseResult.setCode(ResponseResult.SUCCESSCODE);
}catch (Exception e) {
// 捕获异常
}
} /*写seesion,保存当前user对象*/
//从shiro中获取当前用户
User sUser = (User)subject.getPrincipal();
subject.getSession().setAttribute("sUser", sUser);
return responseResult;
}
ShiroAuthorizingRealm:自定义Realm(ShiroAuthorizingRealm.java)
public class ShiroAuthorizingRealm extends AuthorizingRealm {
private static final Logger logger = Logger.getLogger(ShiroAuthorizingRealm.class);
//注入用户管理对象
@Autowired
private UserService userService;
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
// TODO 自动生成的方法存根
return null;
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken uPasswordToken) throws AuthenticationException {
UsernamePasswordToken upToken = (UsernamePasswordToken) uPasswordToken;
String loginName = upToken.getUsername();
String passWord = String.valueOf(upToken.getPassword());
User user = null;
try {
user = userService.findUserByLoginName(loginName);
} catch(Exception ex) {
logger.warn("获取用户失败\n" + ex.getMessage());
}
if (user == null) {
logger.warn("用户不存在");
throw new UnknownAccountException("用户不存在");
}
else if (!passWord.equals(user.getPassWord())) {
logger.warn("密码错误");
throw new UnknownAccountException("密码错误");
}
logger.info("用户【" + loginName + "】登录成功");
AuthenticationInfo authcInfo = new SimpleAuthenticationInfo(user, user.getPassWord(), user.getUserName());
Subject subject1 = SecurityUtils.getSubject();
if (null != subject1) {
Session session = subject1.getSession();
if (null != session) {
session.setAttribute("currentUser", user);
}
}
return authcInfo;
}
}
shiro.xml配置文件:(spring-shiro.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <!-- 缓存管理器 使用Ehcache实现 -->
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:ehcache-shiro.xml" />
</bean> <!-- Shiro的Web过滤器 -->
<!-- 此bean要被web.xml引用,和web.xml中的filtername同名 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<property name="loginUrl" value="/system/login" />
<property name="unauthorizedUrl" value="/" />
<property name="filterChainDefinitions">
<value>
/system/login = anon
</value>
</property>
</bean> <!-- 安全管理器 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="dbRealm" />
<property name="cacheManager" ref="cacheManager"/>
</bean>
<!-- 自定义realm -->
<bean id="dbRealm" class="lee.system.school.shiro.ShiroAuthorizingRealm">
<property name="userService" ref="userService"/>
</bean>
<bean id="userService" class="lee.system.school.service.impl.UserService" /> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
</beans>
web.xml:(web.xml)
<!-- 加载spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml,classpath:spring-mybatis.xml,classpath:spring-shiro.xml</param-value>
</context-param> <!-- 设置监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- Shiro配置(需要 ContextLoaderListener ) -->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
ResponseResult类:(ResponseResult.java)
public class ResponseResult {
/**
* 返回code:成功
*/
public final static int SUCCESSCODE = 1;
/**
* 返回code:失败
*/
public final static int FAILURECODE = 0;
private int code;
private String msg;
private Object data;
public ResponseResult(int code) {
this.code = code;
}
public ResponseResult(int code, String msg) {
this.code = code;
this.msg = msg;
}
public ResponseResult(int code, String msg, Object data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
java-shiro登录验证的更多相关文章
- shiro登录验证简单理解
这两天接手了下师兄的项目,要给系统加个日志管理模块,其中需要记录登录功能的日志,那么首先要知道系统的登录是在哪里实现验证的. 该系统把所有登录验证还有权限控制的工作都交给了shiro. 这篇文章就先简 ...
- SpringMVC+Apache Shiro+JPA(hibernate)案例教学(三)给Shiro登录验证加上验证码
序: 给Shiro加入验证码,有多种方式,当然你也可以通过继承修改FormAuthenticationFilter类,通过Shiro去验证验证码.具体实现请百度: 应用Shiro到Web Applic ...
- shiro登录验证原理
这段时间有点忙,没咋写博客,今天打开staruml看到以前画的一张shiro原理图,先在这发一下,空了再好好进行分析.
- 简单两步快速实现shiro的配置和使用,包含登录验证、角色验证、权限验证以及shiro登录注销流程(基于spring的方式,使用maven构建)
前言: shiro因为其简单.可靠.实现方便而成为现在最常用的安全框架,那么这篇文章除了会用简洁明了的方式讲一下基于spring的shiro详细配置和登录注销功能使用之外,也会根据惯例在文章最后总结一 ...
- Shiro安全框架入门篇(登录验证实例详解与源码)
转载自http://blog.csdn.net/u013142781 一.Shiro框架简单介绍 Apache Shiro是Java的一个安全框架,旨在简化身份验证和授权.Shiro在JavaSE和J ...
- 基于权限安全框架Shiro的登录验证功能实现
目前在企业级项目里做权限安全方面喜欢使用Apache开源的Shiro框架或者Spring框架的子框架Spring Security. Apache Shiro是一个强大且易用的Java安全框架,执行身 ...
- Apache Shiro:【2】与SpringBoot集成完成登录验证
Apache Shiro:[2]与SpringBoot集成完成登录验证 官方Shiro文档:http://shiro.apache.org/documentation.html Shiro自定义Rea ...
- java桥连接sql server之登录验证及对数据库增删改查
一:步骤 1.sql server建立数据库和相关表 2.建立数据源 (1).打开控制面板找到管理,打开ODBC选项或直接搜索数据源 (2).打开数据源配置后点击添加,选择sql server点击 ...
- Java Web Filter登录验证
初做网站需要登录验证,转自 :http://blog.csdn.net/daguanjia11/article/details/48995789 Filter: Filter是服务器端的组件,用来过滤 ...
- Redis 在java中的使用(登录验证,5分钟内连续输错3次密码,锁住帐号,半小时后解封)(三)
在java中使用redis,做简单的登录帐号的验证,使用string类型,使用redis的过期时间功能 1.首先进行redis的jar包的引用,因为用的是springBoot,springBoot集成 ...
随机推荐
- day 74 json 和 ajax 的实例
一 json的定义: json(JavaScript object notation,js对象标记)是一种轻量级的数据交换格式,它基于ecmascript(w3c指定的js规范)的一个子集,采用完全独 ...
- 将python环境打包成.txt文件
1 导出Python环境安装包[root@bogon ~]# pip freeze > packages.txt这将会创建一个 packages.txt文件,其中包含了当前环境中所有包及各自的版 ...
- 爬虫系列4:scrapy技术进阶之多页面爬取
多页面爬取有两种形式. 1)从某一个或者多个主页中获取多个子页面的url列表,parse()函数依次爬取列表中的各个子页面. 2)从递归爬取,这个相对简单.在scrapy中只要定义好初始页面以及爬虫规 ...
- Java的Annotation标签
只需要简单的使用Java的Annotation标签即可将标准的Java方法发布成Web Service,但不是所有的Java类都可以发布成Web Service.Java类若要成为一个实现了Web S ...
- posix信号量与互斥锁
1.简介 POSIX信号量是一个sem_t 类型的变量,但POSIX 有两种信号量的实现机制:无名信号量和命名信号量.无名信号量可以用在共享内存的情况下, 比如实现进程中各个线程之间的互斥和同步.命名 ...
- file类和io流
一.file类 file类是一个可以用其对象表示目录或文件的一个Java.io包中的类 import java.io.File; import java.io.IOException; public ...
- GCC内置函数
在C语言写的程序中,有时候没有包含头文件,直接调用一些函数,如printf,也不会报错,因为GCC内置和一些函数.如果包含了头文件,则去第三方库中链接这个函数,不再使用GCC内置的函数.每个编译器的内 ...
- Python 正则 —— 捕获与分组
\n:表示第 n 个捕获: >> s = "<html><h1>what the fuck!</h1></html>" ...
- ERROR: gnu-config-native-20150728+gitAUTOINC+b576fa87c1-r0 do_unpack: Function failed: Fetcher failure: Fetch command failed with exit code 128, output: fatal: the '--set-upstream' option is no longer
/********************************************************************** * ERROR: gnu-config-native-2 ...
- linux管道命令之head与tail
常常会遇到这样的情况: 1.我训练一个模型需要用到很多图片,这些图片都在一个文件夹下面,但是我想仅仅拷贝个一两张看一下图片的质量怎么样? 2.文件夹下有各种各样的数据,数目非常庞大,我想看一下文件夹下 ...