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. redis问题集

    Redis有哪些数据结构? 字符串String.字典Hash.列表List.集合Set.有序集合SortedSet. 如果你是Redis中高级用户,还需要加上下面几种数据结构HyperLogLog.G ...

  2. 《转》最受欢迎的ASP.NET的CMS下载

    1. Umbraco 项目地址 | 下载 Umbraco是一个开放源码的CMS内容管理系统,基于asp.net建立,使用mssql进行存储数据. 使用Umbraco ,设计师能创造出有效的XHTML标 ...

  3. iOS xcode6.0使用7.1运行程序 iphone5上下有黑条

    转自:http://stackoverflow.com/questions/25817562/black-bars-appear-in-app-when-targeting-ios7-1-or-7-0 ...

  4. Java日期类:Date和Calendar的使用

    总是使用这两个类,总是需要百度.还不如一次全部整理完. 一.介绍: Date 类 Date 表示特定的瞬间,精确到毫秒. 在 JDK 1.1 之前,类 Date 有两个其他的函数.它允许把日期解释为年 ...

  5. POJ 2609 Ferry Loading(双塔DP)

    Ferry Loading Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1807   Accepted: 509   Sp ...

  6. 数据库系统概述(Data Model、DBMS、DBS、RDBS、Structured Query Language)

    数据Data 描述事物的符号记录成为数据. 数据是数据库中存储的基本对象.   除了基本的数字之外.像图书的名称.价格.作者都可以称为数据. 将多种数据记录列成一张表.通过数据表管理数据. 每一行的数 ...

  7. Storm的组件

    摘自网上 当时写的很好,很详细的介绍了各个组件直接的关系 Storm集群和Hadoop集群表面上看很类似.但是Hadoop上运行的是MapReduce jobs,而在Storm上运行的是拓扑(topo ...

  8. attempt to index a nil value (global 'luasql')

    require ’socket‘ require ’luasql.mysql' 上述返回结果都是正常 但是执行 env = luasql.mysql(),报错: stdin:1: attempt to ...

  9. TGI指数

    TGI指数 目标人群中国具有某一特征的群体占比/总体中具有相同特征的群体的占比*标准数100

  10. Ansi、GB2312、GBK、Unicode(utf8、16、32)

    关于ansi,一般默认为本地编码方式,中文应该是gb编码 他们之间的关系在这边文章里描写的很清楚:http://blog.csdn.net/ldanduo/article/details/820353 ...