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的更多相关文章

  1. 基于SSM的单点登陆01

    使用SSM的Maven聚合项目 建立父项目market的pom文件 <?xml version="1.0" encoding="UTF-8"?> & ...

  2. 基于SSM的单点登陆05

    springmvc.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...

  3. 基于SSM的单点登陆03

    TbUser.java和TbUserExample.java,TbUserMapper.java,TbUserMapper.xml由mybatis框架生成. generatorConfig.xml & ...

  4. 基于SSM的单点登陆02

    pom文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http: ...

  5. Spring Security 解析(六) —— 基于JWT的单点登陆(SSO)开发及原理解析

    Spring Security 解析(六) -- 基于JWT的单点登陆(SSO)开发及原理解析   在学习Spring Cloud 时,遇到了授权服务oauth 相关内容时,总是一知半解,因此决定先把 ...

  6. 集成基于OAuth协议的单点登陆

    在之前的一篇文章中,我们已经介绍了如何为一个应用添加对CAS协议的支持,进而使得我们的应用可以与所有基于CAS协议的单点登陆服务通讯.但是现在的单点登陆服务实际上并不全是通过实现CAS协议来完成的.例 ...

  7. 集成基于CAS协议的单点登陆

    相信大家对单点登陆(SSO,Single Sign On)这个名词并不感到陌生吧?简单地说,单点登陆允许多个应用使用同一个登陆服务.一旦一个用户登陆了一个支持单点登陆的应用,那么在进入其它使用同一单点 ...

  8. 基于SAML的单点登录介绍

    http://blog.csdn.net/csethcrm/article/details/20694993 一.背景知识: SAML即安全断言标记语言,英文全称是Security Assertion ...

  9. (转)基于SAML的单点登录介绍

    转:http://www.cnblogs.com/zsuxiong/archive/2011/11/19/2255497.html 一.背景知识: SAML即安全断言标记语言,英文全称是Securit ...

随机推荐

  1. Openstack(Kilo)安装系列之Keystone(四)

    创建租间.用户.角色 一.To configure prerequisites 1.Configure the authentication token: export OS_TOKEN=ADMIN_ ...

  2. mui 单页面下拉刷新

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. JS encodeURI和encodeURIComponent

    一.最常用的encodeURI和encodeURIComponent 对URL编码是常见的事,所以这两个方法应该是实际中要特别注意的.它们都是编码URL,唯一区别就是编码的字符范围,其中encodeU ...

  4. [Linux 学习] Centos 图形和命令行界面切换

    要想切换图形界面和命令行界面切换, 需修改一个文件... root 下, 到 /etc/inittab 修改 id:5:initdefault Linux系统有7个运行级别(runlevel): 运行 ...

  5. CentOS 安装 dotnetcore

    参考官方教程:https://www.microsoft.com/net/core#linuxcentos 安装.NET CORE SDK sudo yum install libunwind lib ...

  6. 1.2_php验证码

    使用php生成动态的验证码图片 <!DOCTYPE html> <html> <head> <meta charset="utf-8"&g ...

  7. maven pom.xml常用标签 Exclusions plugins是什么意思

    Exclusions maven的依赖(dependencies)有传递性,为了解决兼容性问题,就用exclusions来排除造成兼容性问题的依赖. 写法如下: 加入项目A依赖项目B,项目B依赖项目C ...

  8. VC的CListCtrl控件

    1. CListCtrl 样式及设置 2. 扩展样式设置 3. 数据插入 4. 一直选中Item 5. 选中和取消选中Item 6. 得到CListCtrl中所有行的checkbox的状态 7. 得到 ...

  9. GO语言中使用OpenCV

    GO语言中使用OpenCV - OpenCV China :图像处理,计算机视觉库,Image Processing, Computer Vision http://wiki.opencv.org.c ...

  10. tortoisegit错误: disconnected - no supported authentication methods available(server sent: publickey)

    修改小乌龟的 SSH客户端: