之前实现的登录和注册是在同一个tomcat内部完成,我们现在的系统架构是每一个系统都是由一个团队进行维护,每个系统都是单独部署运行一个单独的tomcat,所以,不能将用户的登录信息保存到session中(多个tomcat的session是不能共享的),所以我们需要一个单独的系统来维护用户的登录信息。
<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.0modelVersion>
<parent>
<groupId>com.taotao.parentgroupId>
<artifactId>taotao-parentartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<groupId>com.taotao.ssogroupId>
<artifactId>taotao-ssoartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>warpackaging>
<dependencies>
<dependency>
<groupId>com.taotao-commongroupId>
<artifactId>taotao-commonartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aspectsartifactId>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
dependency>
<dependency>
<groupId>com.github.abel533groupId>
<artifactId>mapperartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
dependency>
<dependency>
<groupId>com.jolboxgroupId>
<artifactId>bonecp-springartifactId>
dependency>
<dependency>
<groupId>jstlgroupId>
<artifactId>jstlartifactId>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jsp-apiartifactId>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-lang3artifactId>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-ioartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<configuration>
<port>8083port>
<path>/path>
configuration>
plugin>
plugins>
build>
project>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>taotao-ssodisplay-name>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring/applicationContext*.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<filter>
<filter-name>encodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>encodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<servlet>
<servlet-name>taotao-ssoservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring/taotao-sso-servlet.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>taotao-ssoservlet-name>
<url-pattern>*.htmlurl-pattern>
servlet-mapping>
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
welcome-file-list>
web-app>
方案:
1、 将taotao-web中的js和css拷贝到taotao-sso中
a) 好处:简单、方便
b) 缺点:重复、对用户而言需要重复加载
2、 将taotao-sso中的引用指向www.taotao.com
a) 好处:对用户而言只需要加载一次即可
b) 缺点:修改页面
方案三:
需要通过nginx访问静态资源,JS、CSS、Image。
具体实现:
1、 使用新域名访问静态资源 static.taotao.com
a) 好处:避免携带一些无用的cookie
2、 拷贝JS和CSS到磁盘路径中:
2015-11-19 11:09:57,870 [http-bio-8083-exec-2] [org.springframework.jdbc.datasource.DataSourceTransactionManager]-[DEBUG] Releasing JDBC Connection [ConnectionHandle{url=jdbc:mysql://127.0.0.1:3306/taotao?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true, user=root, debugHandle=null, lastResetAgoInSec=0, lastUsedAgoInSec=0, creationTimeAgoInSec=0}] after transaction
2015-11-19 11:09:57,870 [http-bio-8083-exec-2] [org.springframework.jdbc.datasource.DataSourceUtils]-[DEBUG] Returning JDBC Connection to DataSource
2015-11-19 11:09:57,891 [http-bio-8083-exec-2] [org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver]-[DEBUG] Resolving exception from handler [public org.springframework.http.ResponseEntity com.taotao.sso.controller.UserController.check(java.lang.String,java.lang.Integer)]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
2015-11-19 11:09:57,893 [http-bio-8083-exec-2] [org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver]-[DEBUG] Resolving exception from handler [public org.springframework.http.ResponseEntity com.taotao.sso.controller.UserController.check(java.lang.String,java.lang.Integer)]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
2015-11-19 11:09:57,893 [http-bio-8083-exec-2] [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver]-[DEBUG] Resolving exception from handler [public org.springframework.http.ResponseEntity com.taotao.sso.controller.UserController.check(java.lang.String,java.lang.Integer)]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
2015-11-19 11:09:57,893 [http-bio-8083-exec-2] [org.springframework.web.servlet.DispatcherServlet]-[DEBUG] Null ModelAndView returned to DispatcherServlet with name 'taotao-sso': assuming HandlerAdapter completed request handling
2015-11-19 11:09:57,893 [http-bio-8083-exec-2] [org.springframework.web.servlet.DispatcherServlet]-[DEBUG] Successfully completed request
最终原因:
SpringMVC的规定:在SpringMVC中如果请求以html结尾,那么就不会返回JSON数据。
获取到到的127.0.0.1,需要将cookie写入到taotao.com中,这样违反了浏览的安全的原则,导致写入失败。
解决方案:
只需要通过request对象获取到正确的地址(xxx.taotao.com)即可。
请求:用户请求 -> nginx -> tomcat,tomcat根本不知道真正的请求来源。
总结:
二级域名可以将cookie写入到主域名下。 A.taotao.com -> taotao.com
二级域名之间不能互相写入,a.taotao.com 不能写入到 b.taotao.com