单点登录

单点登录
所谓的单点登录系统,就是一个分布式项目中,统一认证平台。单点登录实现了,只有登录一次,就可以使用统一认证的信息访问分布式系统的所有系统。
传统登录方式的缺陷
我们知道,登录的时候是将用户信息,写入session。
但是,在电商系统中,为了解决项目的并发能力,往往需要将项目部署到多个服务器上,用负载均衡处理。


如果在登录的时候,把用户信息写入session中,就会因为多个session的问题,导致登录认证失败
如何解决这个问题呢?
我们需要使用单点登录功能。单点登录就是借助唯一的cookie来实现。


搭建单点登录系统
思路:(1)创建系统。
(2)实现注册。
(3)实现登录。




第一部分:创建单点登录系统
思路:(1)创建项目
(2)导入依赖
(3)整合SSM框架
(4)实现注册
(5)实现登录


第一步:使用Maven创建项目(war模型)




第二步;导入jar依赖
导包说明:
ego-base
Spring核心包
Springmvc依赖包
Mybatis核心包
事物依赖包+切面
Jdbc驱动+连接池druid
Json依赖包
Jsp依赖
导入插件:tomcat插件


"http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0

cn.gzsxt.ego
ego-parent
1.0

cn.gzsxt.ego
ego-sso
1.0
war




cn.gzsxt.ego
ego-base
1.0





org.springframework
spring-context


org.springframework
spring-beans



org.springframework
spring-jdbc




org.springframework
spring-aspects



org.springframework
spring-webmvc




jstl
jstl


javax.servlet
servlet-api
provided


javax.servlet
jsp-api
provided



mysql
mysql-connector-java




com.alibaba
druid



org.mybatis
mybatis-spring



com.fasterxml.jackson.core
jackson-databind







org.apache.tomcat.maven
tomcat7-maven-plugin

8084
/
UTF-8







第三步:导入静态资源+jsp页面


第四步:配置SpringMVC核心控制器
创建web.xml文件。
"1.0" encoding="UTF-8"?>
"2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_2_5.xsd ">



characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter

encoding
utf-8



characterEncodingFilter
/*




dispatcherServlet
org.springframework.web.servlet.DispatcherServlet

contextConfigLocation
classpath:spring-*.xml

1


dispatcherServlet


/user/*




第五步:Spring整合SpringMVC
创建springmvc.xml文件。
"1.0" encoding="UTF-8"?>
"http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">




"cn.gzsxt.sso"/>





"org.springframework.web.servlet.view.InternalResourceViewResolver">
"prefix" value=https://www.it610.com/article/"/WEB-INF/jsp/">
"suffix" value=https://www.it610.com/article/".jsp">






第六步:Spring整合Mybatis
(1)创建resource.proteries文件,配置数据库信息
#配置数据源
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ego
jdbc.username=root
jdbc.password=gzsxt


(2)创建spring-data.xml文件。
"1.0" encoding="UTF-8"?>
"http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">


"utf-8" location="classpath:*.properties"/>




"dataSource" class="com.alibaba.druid.pool.DruidDataSource">

"driverClassName" value=https://www.it610.com/article/"${driver.driverClassName}"/>
"url" value=https://www.it610.com/article/"${driver.url}"/>
"username" value=https://www.it610.com/article/"${driver.username}"/>
"password" value=https://www.it610.com/article/"${driver.password}"/>


"maxActive" value=https://www.it610.com/article/"10"/>
"minIdle" value=https://www.it610.com/article/"2"/>



"sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
"dataSource" ref="dataSource"/>


"mapperLocations" value=https://www.it610.com/article/"classpath:cn/gzsxt/manager/mapper/*Mapper.xml"/>



"mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">


"sqlSessionFactoryBeanName" value=https://www.it610.com/article/"sqlSessionFactoryBean"/>


"basePackage" value=https://www.it610.com/article/"cn.gzsxt.manager.mapper"/>





"transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
"dataSource" ref="dataSource">





"tx" transaction-manager="transactionManager">

"select*" read-only="true"/>
"get*" read-only="true"/>
"find*" read-only="true"/>


"insert*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="EXCEPTION"/>
"update*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="EXCEPTION"/>
"delete*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="EXCEPTION"/>





"execution( * cn.gzsxt.sso.service.*.*(..))" id="pointcut"/>
"tx" pointcut-ref="pointcut"/>




第七步:测试
需求:创建PageController类,访问注册登陆页面。
package cn.gzsxt.sso.controller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class PageController {


@RequestMapping("/showRegister")
public String showRegister(){
return "register";
}
@RequestMapping("/showLogin")
public String showLogin(){
return "login";
}
【单点登录】}

    推荐阅读