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. 【R】自定义描述统计函数-从均值到峰度偏度

    data_show<-function(x) { n<-length(x) m<-mean(x) v<-var(x) s<-sd(x) me<-median(x) ...

  2. MathType编辑书名号的方法有哪些

    MathType作为一款数学公式编辑器,其功能是非常全面的,大家都知道它可以非常方便的编辑各种数据公式和符号.比如书名号.对于这个符号大家的印象是在文科方面用得比较多,其实在数学领域也是常常会用到的, ...

  3. angular_文本变化

    注意,在input中用ng-change的时候,一定要结合着ng-model用 开头,注意在这里添加了ng-app <!DOCTYPE html> <html lang=" ...

  4. iOS 百度地图获取当前地理位置

    // // ViewController.m // BaiDuDemo // // Created by Chocolate. on 15-3-2. // Copyright (c) 2015年 re ...

  5. springmvc的一个错误

    初学ssm,碰到一个错误,非注解的就没错,注解的就错了 找了半天,终于发现错误,真的很傻比啊,导入的springframework版本不一致... 都导入4.3.18版本的即可...

  6. 160720、SSM-Shiro使用详解

    前言 相比有做过企业级开发的童鞋应该都有做过权限安全之类的功能吧,最先开始我采用的是建用户表,角色表,权限表,之后在拦截器中对每一个请求进行拦截,再到数据库中进行查询看当前用户是否有该权限,这样的设计 ...

  7. Jupyter Notebook远程服务器配置

    首先要生成密码,打开python终端. In [1]: from IPython.lib import passwd In [2]: passwd() Enter password: Verify p ...

  8. 支付宝SDK的使用方法

    前奏 现在随着移动开发的快速发展,越来越多的应用要求在线支付功能.最近做了一个关于支付宝支付功能的应用,在使用支付宝的过程中,遇到一些不必要的弯路,因此,写了这篇文章总结一下关于ios开发如何使用支付 ...

  9. 当集群为red或者yellow的时候怎么办

    百度Elasticsearch-产品描述-介绍-百度云 https://cloud.baidu.com/doc/BES/FAQ.html#.30.1B.42.A0.26.0A.6E.B7.F1.84. ...

  10. ubuntu su failure when password was right

    https://blog.csdn.net/u013066244/article/details/52694540