基于SSM的单点登陆04

jdbc.properties
JDBC_DRIVER=org.mariadb.jdbc.Driver
JDBC_URL=jdbc:mariadb://127.0.0.1:3306/market
JDBC_USERNAME=root
JDBC_PASSWORD=root
sso.properties
#标记session的token名称
REDIS_USER_SESSION_KEY=GSESSION
#session超时时间
TOKEN_TIME_OUT=1800
mybatis-pageHelper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 配置分页插件 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->
<property name="dialect" value="mariadb"/>
</plugin>
</plugins>
</configuration>
spring-mybatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 解析properties文件的工具类 -->
<context:property-placeholder location="classpath:*.properties"/> <!-- 开启service层的注解扫描 -->
<context:component-scan base-package="io.guangsoft.market.service"/> <!-- 配置数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="driverClassName" value="${JDBC_DRIVER}" />
<property name="url" value="${JDBC_URL}" />
<property name="username" value="${JDBC_USERNAME}" />
<property name="password" value="${JDBC_PASSWORD}" />
<property name="maxActive" value="10" />
<property name="minIdle" value="5" />
</bean> <!-- sqlSessionfactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-pagehelper.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean> <!-- 扫描代理类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="io.guangsoft.market.dao"/>
</bean> <!-- 配置事物管理器的切面 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> <!-- 通知 -->
<tx:advice id="Advice" transaction-manager="transactionManager">
<!-- 事物传播行为 -->
<tx:attributes>
<!-- 传播行为 -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="bat*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="drop*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor advice-ref="Advice" pointcut="execution(* io.guangsoft.market.service.*.*(..))" />
</aop:config>
</beans>
UserService.jsva
package io.guangsoft.market.service; import io.guangsoft.market.util.bean.GResult;
import io.guangsoft.market.dao.bean.TbUser; public interface UserService {
public GResult userParamCheck(String param, int type); public GResult userRegister(TbUser user); public GResult userLogin(String username, String password); public GResult findUserByToken(String token);
}
UserServiceImpl.java
package io.guangsoft.market.service; import io.guangsoft.market.util.bean.GResult;
import io.guangsoft.market.dao.bean.TbUser;
import io.guangsoft.market.dao.bean.TbUserExample;
import io.guangsoft.market.dao.jedis.JedisDao;
import io.guangsoft.market.util.utils.JsonUtil;
import io.guangsoft.market.dao.mapper.TbUserMapper;
import io.guangsoft.market.util.utils.GResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils; import java.util.Date;
import java.util.List;
import java.util.UUID; @Service
public class UserServiceImpl implements UserService { @Autowired
private TbUserMapper userMapper; @Autowired
private JedisDao jedisDao; @Value("${REDIS_USER_SESSION_KEY}")
private String REDIS_USER_SESSION_KEY; @Value("${TOKEN_TIME_OUT}")
private int TOKEN_TIME_OUT; /**
* 用户注册时数据校验
*/
@Override
public GResult userParamCheck(String param, int type) {
TbUserExample example = new TbUserExample();
TbUserExample.Criteria c = example.createCriteria();
//条件的指定
if (type == 1) { //用户名
c.andUsernameEqualTo(param);
} else if (type == 2) { //判断手机号是否存在
c.andPhoneEqualTo(param);
} else { //判断邮箱是否可用
c.andEmailEqualTo(param);
}
List<TbUser> list = this.userMapper.selectByExample(example);
if (list == null || list.size() <= 0) {
// 返回数据,true:数据可用,false:数据不可用
return GResultUtil.success();
}
return GResultUtil.fail();
} /**
* 用户注册
*/
@Override
public GResult userRegister(TbUser user) {
//1.数据补齐
user.setCreated(new Date());
user.setUpdated(new Date());
//2.将密码做加密处理 我们可以使用DigestUtils spring提供的一个工具类 来做md5的加密处理
user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
this.userMapper.insert(user);
return GResultUtil.success();
} /**
* 用户登陆
*/
@Override
public GResult userLogin(String username, String password) {
TbUserExample example = new TbUserExample();
TbUserExample.Criteria c = example.createCriteria();
c.andUsernameEqualTo(username);
//执行查询
List<TbUser> list = this.userMapper.selectByExample(example);
//判断用户名
if (list == null || list.size() <= 0) {
return GResultUtil.fail(-1, "用户名或密码有误!");
}
//判断密码
TbUser user = list.get(0);
if (!user.getPassword().equals(DigestUtils.md5DigestAsHex(password.getBytes()))) {
return GResultUtil.fail(-1, "用户名或密码有误!");
}
//用户名与密码都OK
//生成token 这个token就是我们来源传统登录中的sessionID
String uuid = UUID.randomUUID().toString();
//将生成的token作为key与user对象转换完的json格式字符串写入到redis中
//需要将user的密码置空
user.setPassword(null);
String userJson = JsonUtil.objectToJson(user);
String token = this.REDIS_USER_SESSION_KEY + ":" + uuid;
this.jedisDao.set(this.REDIS_USER_SESSION_KEY + ":" + token, userJson);
this.jedisDao.expire(this.REDIS_USER_SESSION_KEY + ":" + token, this.TOKEN_TIME_OUT);
return GResultUtil.build(0, "用户正确!", token);
} /**
* 根据token查询用户
*/
@Override
public GResult findUserByToken(String uuid) {
String str = this.jedisDao.get(this.REDIS_USER_SESSION_KEY+":"+uuid);
//判断token是否失效
if(StringUtils.isBlank(str)){
return GResultUtil.fail(-1, "token已失效!");
}
//如果用户的token没有失效,需要重置失效时间
this.jedisDao.expire(this.REDIS_USER_SESSION_KEY+":"+uuid, this.TOKEN_TIME_OUT);
TbUser user = JsonUtil.jsonToPojo(str, TbUser.class);
return GResultUtil.build(0, "查询成功!", user);
}
}
基于SSM的单点登陆04的更多相关文章
- 基于SSM的单点登陆01
使用SSM的Maven聚合项目 建立父项目market的pom文件 <?xml version="1.0" encoding="UTF-8"?> & ...
- 基于SSM的单点登陆05
springmvc.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...
- 基于SSM的单点登陆03
TbUser.java和TbUserExample.java,TbUserMapper.java,TbUserMapper.xml由mybatis框架生成. generatorConfig.xml & ...
- 基于SSM的单点登陆02
pom文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http: ...
- Spring Security 解析(六) —— 基于JWT的单点登陆(SSO)开发及原理解析
Spring Security 解析(六) -- 基于JWT的单点登陆(SSO)开发及原理解析 在学习Spring Cloud 时,遇到了授权服务oauth 相关内容时,总是一知半解,因此决定先把 ...
- 集成基于OAuth协议的单点登陆
在之前的一篇文章中,我们已经介绍了如何为一个应用添加对CAS协议的支持,进而使得我们的应用可以与所有基于CAS协议的单点登陆服务通讯.但是现在的单点登陆服务实际上并不全是通过实现CAS协议来完成的.例 ...
- 集成基于CAS协议的单点登陆
相信大家对单点登陆(SSO,Single Sign On)这个名词并不感到陌生吧?简单地说,单点登陆允许多个应用使用同一个登陆服务.一旦一个用户登陆了一个支持单点登陆的应用,那么在进入其它使用同一单点 ...
- 基于SAML的单点登录介绍
http://blog.csdn.net/csethcrm/article/details/20694993 一.背景知识: SAML即安全断言标记语言,英文全称是Security Assertion ...
- (转)基于SAML的单点登录介绍
转:http://www.cnblogs.com/zsuxiong/archive/2011/11/19/2255497.html 一.背景知识: SAML即安全断言标记语言,英文全称是Securit ...
随机推荐
- 【Python】IDLE清屏
上网搜,没搜到可用的快捷键.但看到一个通过打印空内容来清屏的方法,smart ef clear(): for i in range(60): print
- iOS -转载-字符串是否为空判断方法
- (BOOL)blankString{ if (![self isKindOfClass:[NSString class]] ){ return YES; } if ([self isEqual:[ ...
- SQL Server Profiler的简单使用(监控mssql)
SQL Server Profiler可以检测在数据上执行的语句,特别是有的项目不直接使用sql语句,直接使用ORM框架的系统处理数据库的项目,在调试sql语句时,给了很大的帮助. 之前写了使用SQL ...
- python bottle学习(一)快速入门
from bottle import (run, route, get, post, put, delete) # bottle中添加路由的两种方法 # 第一种,使用route装饰器,需要指定meth ...
- xcode 运行 lua版本崩溃 解决方案
问题描述:运行到LuaStack::init() 崩溃 原因: luajit不支持arm64 解决方案:编译luajit64位静态库 a.可以直接下载别人编译好的库,然后直接覆盖cocos2d\ext ...
- http协议详解(2)
HTTP报文是面向文本的,报文中的每一个字段都是一些ASCII码串,各个字段的长度是不确定的.HTTP有两类报文:请求报文和响应报文. HTTP请求报文 一个HTTP请求报文由请求行(request ...
- DES加密原理
DES加密步奏: 1.初始化两个字符串,一个为指定的秘钥,一个为初始化向量,要求是8个字符. 2.加密:秘钥.向量.需加密的字符串传换成byte[]类型: 声明加密标准类,DESCryptoServi ...
- SOE 中调用第三方dll
一.简介 在利用soe实现server的扩展的时候,有些时候,需要调用第三方的dll库.官网中给出了明确的说明,soe中是可以添加第三方的dll文件,但是一直没有测试.按照官方的步骤应该是一个非常的简 ...
- Python元组组成的列表转化为字典
虽然元组.列表不可以直接转化为字典,但下面的确是可行的,因为经常用python从数据库中读出的是元组形式的数据. # 原始数据 rows = (('apollo', 'male', '164.jpeg ...
- pip安装lxml报错 Fatal error in launcher: Unable to create process using '"c:\users\administrator\appdata\local\programs\python\python36\python.exe" "C:\Users\Administrator\AppData\L
pip install lxml 安装报错 E:\apollo\spider_code>Fatal error in launcher: Unable to create process usi ...