基於SSM的單點登錄04

jdbc.propertiesjava

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.propertiesgit

1 #標記session的token名稱 2 REDIS_USER_SESSION_KEY=GSESSION 3 #session超時時間 4 TOKEN_TIME_OUT=1800

mybatis-pageHelper.xmlgithub

 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.xmlredis

 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.jsvaspring

 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.javasql

 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 }
相關文章
相關標籤/搜索