基于SSM的单点登陆04

Posted guanghe

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于SSM的单点登陆04相关的知识,希望对你有一定的参考价值。

技术分享图片

jdbc.properties

1 JDBC_DRIVER=org.mariadb.jdbc.Driver
2 JDBC_URL=jdbc:mariadb://127.0.0.1:3306/market
3 JDBC_USERNAME=root
4 JDBC_PASSWORD=root

sso.properties

1 #标记session的token名称
2 REDIS_USER_SESSION_KEY=GSESSION
3 #session超时时间
4 TOKEN_TIME_OUT=1800

mybatis-pageHelper.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE configuration
 3         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6 <!-- 配置分页插件 -->
 7     <plugins>
 8         <plugin interceptor="com.github.pagehelper.PageHelper">
 9             <!-- 设置数据库类型 Oracle,mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->        
10             <property name="dialect" value="mariadb"/>
11         </plugin>
12     </plugins>
13 </configuration>

spring-mybatis.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
 4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 7     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
 8     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
 9     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
10     <!-- 解析properties文件的工具类 -->
11     <context:property-placeholder location="classpath:*.properties"/>
12 
13     <!-- 开启service层的注解扫描 -->
14     <context:component-scan base-package="io.guangsoft.market.service"/>
15 
16     <!-- 配置数据源 -->
17     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"  destroy-method="close">
18         <property name="driverClassName" value="${JDBC_DRIVER}" />
19         <property name="url" value="${JDBC_URL}" />
20         <property name="username" value="${JDBC_USERNAME}" />
21         <property name="password" value="${JDBC_PASSWORD}" />
22         <property name="maxActive" value="10" />
23         <property name="minIdle" value="5" />
24     </bean>
25     
26     <!-- sqlSessionfactory -->
27     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
28         <property name="configLocation" value="classpath:mybatis-pagehelper.xml"/>
29         <property name="dataSource" ref="dataSource"/>
30     </bean>
31     
32     <!-- 扫描代理类 -->
33     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
34         <property name="basePackage" value="io.guangsoft.market.dao"/>
35     </bean>
36 
37     <!-- 配置事物管理器的切面 -->
38     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
39         <property name="dataSource" ref="dataSource"/>
40     </bean>
41 
42     <!-- 通知 -->
43     <tx:advice id="Advice" transaction-manager="transactionManager">
44         <!-- 事物传播行为 -->
45         <tx:attributes>
46             <!-- 传播行为 -->
47             <tx:method name="save*" propagation="REQUIRED" />
48             <tx:method name="insert*" propagation="REQUIRED" />
49             <tx:method name="add*" propagation="REQUIRED" />
50             <tx:method name="create*" propagation="REQUIRED" />
51             <tx:method name="delete*" propagation="REQUIRED" />
52             <tx:method name="bat*" propagation="REQUIRED" />
53             <tx:method name="update*" propagation="REQUIRED" />
54             <tx:method name="drop*" propagation="REQUIRED" />
55             <tx:method name="modify*" propagation="REQUIRED" />
56             <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
57             <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
58             <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
59         </tx:attributes>
60     </tx:advice>
61     <aop:config>
62         <aop:advisor advice-ref="Advice" pointcut="execution(* io.guangsoft.market.service.*.*(..))" />
63     </aop:config>
64 </beans>

UserService.jsva

 1 package io.guangsoft.market.service;
 2 
 3 import io.guangsoft.market.util.bean.GResult;
 4 import io.guangsoft.market.dao.bean.TbUser;
 5 
 6 
 7 public interface UserService {
 8     public GResult userParamCheck(String param, int type);
 9 
10     public GResult userRegister(TbUser user);
11 
12     public GResult userLogin(String username, String password);
13 
14     public GResult findUserByToken(String token);
15 }

UserServiceImpl.java

  1 package io.guangsoft.market.service;
  2 
  3 import io.guangsoft.market.util.bean.GResult;
  4 import io.guangsoft.market.dao.bean.TbUser;
  5 import io.guangsoft.market.dao.bean.TbUserExample;
  6 import io.guangsoft.market.dao.jedis.JedisDao;
  7 import io.guangsoft.market.util.utils.JsonUtil;
  8 import io.guangsoft.market.dao.mapper.TbUserMapper;
  9 import io.guangsoft.market.util.utils.GResultUtil;
 10 import org.apache.commons.lang3.StringUtils;
 11 import org.springframework.beans.factory.annotation.Autowired;
 12 import org.springframework.beans.factory.annotation.Value;
 13 import org.springframework.stereotype.Service;
 14 import org.springframework.util.DigestUtils;
 15 
 16 import java.util.Date;
 17 import java.util.List;
 18 import java.util.UUID;
 19 
 20 @Service
 21 public class UserServiceImpl implements UserService {
 22 
 23     @Autowired
 24     private TbUserMapper userMapper;
 25 
 26     @Autowired
 27     private JedisDao jedisDao;
 28 
 29     @Value("${REDIS_USER_SESSION_KEY}")
 30     private String REDIS_USER_SESSION_KEY;
 31 
 32     @Value("${TOKEN_TIME_OUT}")
 33     private int TOKEN_TIME_OUT;
 34 
 35     /**
 36      * 用户注册时数据校验
 37      */
 38     @Override
 39     public GResult userParamCheck(String param, int type) {
 40         TbUserExample example = new TbUserExample();
 41         TbUserExample.Criteria c = example.createCriteria();
 42         //条件的指定
 43         if (type == 1) { //用户名
 44             c.andUsernameEqualTo(param);
 45         } else if (type == 2) { //判断手机号是否存在
 46             c.andPhoneEqualTo(param);
 47         } else { //判断邮箱是否可用
 48             c.andEmailEqualTo(param);
 49         }
 50         List<TbUser> list = this.userMapper.selectByExample(example);
 51         if (list == null || list.size() <= 0) {
 52             // 返回数据,true:数据可用,false:数据不可用
 53             return GResultUtil.success();
 54         }
 55         return GResultUtil.fail();
 56     }
 57 
 58     /**
 59      * 用户注册
 60      */
 61     @Override
 62     public GResult userRegister(TbUser user) {
 63         //1.数据补齐
 64         user.setCreated(new Date());
 65         user.setUpdated(new Date());
 66         //2.将密码做加密处理  我们可以使用DigestUtils spring提供的一个工具类 来做md5的加密处理
 67         user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
 68         this.userMapper.insert(user);
 69         return GResultUtil.success();
 70     }
 71 
 72     /**
 73      * 用户登陆
 74      */
 75     @Override
 76     public GResult userLogin(String username, String password) {
 77         TbUserExample example = new TbUserExample();
 78         TbUserExample.Criteria c = example.createCriteria();
 79         c.andUsernameEqualTo(username);
 80         //执行查询
 81         List<TbUser> list = this.userMapper.selectByExample(example);
 82         //判断用户名
 83         if (list == null || list.size() <= 0) {
 84             return GResultUtil.fail(-1, "用户名或密码有误!");
 85         }
 86         //判断密码
 87         TbUser user = list.get(0);
 88         if (!user.getPassword().equals(DigestUtils.md5DigestAsHex(password.getBytes()))) {
 89             return GResultUtil.fail(-1, "用户名或密码有误!");
 90         }
 91         //用户名与密码都OK
 92         //生成token  这个token就是我们来源传统登录中的sessionID
 93         String uuid = UUID.randomUUID().toString();
 94         //将生成的token作为key与user对象转换完的json格式字符串写入到redis中
 95         //需要将user的密码置空
 96         user.setPassword(null);
 97         String userJson = JsonUtil.objectToJson(user);
 98         String token = this.REDIS_USER_SESSION_KEY + ":" + uuid;
 99         this.jedisDao.set(this.REDIS_USER_SESSION_KEY + ":" + token, userJson);
100         this.jedisDao.expire(this.REDIS_USER_SESSION_KEY + ":" + token, this.TOKEN_TIME_OUT);
101         return GResultUtil.build(0, "用户正确!", token);
102     }
103 
104     /**
105      * 根据token查询用户
106      */
107     @Override
108     public GResult findUserByToken(String uuid) {
109         String str = this.jedisDao.get(this.REDIS_USER_SESSION_KEY+":"+uuid);
110         //判断token是否失效
111         if(StringUtils.isBlank(str)){
112             return GResultUtil.fail(-1, "token已失效!");
113         }
114         //如果用户的token没有失效,需要重置失效时间
115         this.jedisDao.expire(this.REDIS_USER_SESSION_KEY+":"+uuid, this.TOKEN_TIME_OUT);
116         TbUser user = JsonUtil.jsonToPojo(str, TbUser.class);
117         return GResultUtil.build(0, "查询成功!", user);
118     }
119 }

 

以上是关于基于SSM的单点登陆04的主要内容,如果未能解决你的问题,请参考以下文章

基于SSM的单点登陆05

基于SSM的单点登陆02

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

认证和SSO-基于OAuth2单点登陆基本架构

ssm拦截器的原理及实现

基于SSM实现宠物领养系统