单点登录
所谓的单点登录系统,就是一个分布式项目中,统一认证平台。单点登录实现了,只有登录一次,就可以使用统一认证的信息访问分布式系统的所有系统。
传统登录方式的缺陷
我们知道,登录的时候是将用户信息,写入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插件
<project xmlns="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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.gzsxt.ego</groupId>
<artifactId>ego-parent</artifactId>
<version>1.0</version>
</parent>
<groupId>cn.gzsxt.ego</groupId>
<artifactId>ego-sso</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>cn.gzsxt.ego</groupId>
<artifactId>ego-base</artifactId>
<version>1.0</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<!-- spring jdbc+事物 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<!-- springmvc依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- jdbc驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<!-- mybatis整合spring依赖包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</dependency>
<!-- Jackson Json处理工具包-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<port>8084</port>
<path>/</path>
<uriEncoding>UTF-8</uriEncoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
第三步:导入静态资源+jsp页面
第四步:配置SpringMVC核心控制器
创建web.xml文件。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="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 ">
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/user/*</url-pattern>
</servlet-mapping>
</web-app>
第五步:Spring整合SpringMVC
创建springmvc.xml文件。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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">
<context:component-scan base-package="cn.gzsxt.sso"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
第六步: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文件。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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">
<context:property-placeholder file-encoding="utf-8" location="classpath:*.properties"/>
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!-- jdbc四要素,必须配置-->
<property name="driverClassName" value="${driver.driverClassName}"/>
<property name="url" value="${driver.url}"/>
<property name="username" value="${driver.username}"/>
<property name="password" value="${driver.password}"/>
<property name="maxActive" value="10"/>
<property name="minIdle" value="2"/>
</bean>
<bean name="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:cn/gzsxt/manager/mapper/*Mapper.xml"/>
</bean>
<bean name="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
<property name="basePackage" value="cn.gzsxt.manager.mapper"/>
</bean>
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="tx" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="select*" read-only="true"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="insert*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="EXCEPTION"/>
<tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="EXCEPTION"/>
<tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="EXCEPTION"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution( * cn.gzsxt.sso.service.*.*(..))" id="pointcut"/>
<aop:advisor advice-ref="tx" pointcut-ref="pointcut"/>
</aop:config>
</beans>
第七步:测试
需求:创建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";
}
}