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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 解析properties文件的工具类 -->
<context:property-placeholder location="classpath:*.properties"/> <!-- 扫描controller组件 -->
<context:component-scan base-package="io.guangsoft.market.controller"/> <!-- 开启注解驱动 -->
<mvc:annotation-driven /> <!-- 配置视图解析 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 资源映射 -->
<mvc:resources location="/css/" mapping="/css/**"/>
<mvc:resources location="/js/" mapping="/js/**"/>
<mvc:resources location="/images/" mapping="/images/**"/>
</beans>

web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 启动spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 解决post乱码 -->
<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> <!-- 启动springmvc -->
<servlet>
<servlet-name>market</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>market</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

PageController.java

 package io.guangsoft.market.controller;

 import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; /**
* 页面跳转
*/
@Controller
public class PageController { @RequestMapping("/login")
public String showLogin(String redirect, Model model) {
model.addAttribute("redirect", redirect);
return "login";
} @RequestMapping("/register")
public String showRegister() {
return "register";
}
}

UserController.java

 package io.guangsoft.market.controller;

 import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import io.guangsoft.market.util.bean.GResult;
import io.guangsoft.market.dao.bean.TbUser;
import io.guangsoft.market.service.UserService;
import io.guangsoft.market.util.utils.CookieUtil;
import io.guangsoft.market.util.utils.GResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; /**
* 用户登录服务controller
*/
@Controller
@RequestMapping("/user")
public class UserController { @Autowired
private UserService userService; @Value("${REDIS_USER_SESSION_KEY}")
private String cookieName; /**
* 用户注册数据校验接口
*/
@RequestMapping(value="/check/{param}/{type}",method=RequestMethod.GET)
@ResponseBody
public Object checkData(@PathVariable String param,@PathVariable int type,String callback){
GResult result = null;
//数据校验
if(type != 1 && type != 2 && type != 3){
result = GResultUtil.fail(-1, "参数类型有误!");
}else{
//调用业务层做数据校验
result = this.userService.userParamCheck(param, type);
}
//判断是否使用jsonp调用方式
if(callback != null && callback.length() > 0){
MappingJacksonValue mapping = new MappingJacksonValue(result);
mapping.setJsonpFunction(callback);
return mapping;
}
return result;
} /**
* 用户注册接口
*/
@RequestMapping(value="/register",method=RequestMethod.POST)
@ResponseBody
public GResult userRegister(TbUser user){
try{
return this.userService.userRegister(user);
}catch(Exception e){
e.printStackTrace();
return GResultUtil.fail(-1, "注册失败. 请校验数据后请再提交数据!");
}
} /**
* 用户登录接口
*/
@RequestMapping(value="/login",method=RequestMethod.POST)
@ResponseBody
public GResult userLogin(String username,String password,HttpServletRequest request,HttpServletResponse repsonse){
try{
GResult result = this.userService.userLogin(username, password);
if(result.getgCode() == 0) {
CookieUtil.setCookie(request, repsonse, cookieName, result.getgData().toString());
}
return result;
}catch(Exception e){
e.printStackTrace();
return GResultUtil.fail(-1, e.getMessage());
}
} /**
* 根据token查询用户
*/
@RequestMapping(value="/token/{token}",method=RequestMethod.GET)
@ResponseBody
public Object findUserByToken(@PathVariable String token,String callback){
GResult result = this.userService.findUserByToken(token);
//判断是否含有jsonp
if(!StringUtils.isBlank(callback)){
MappingJacksonValue mapping = new MappingJacksonValue(result);
mapping.setJsonpFunction(callback);
return mapping;
}
return result;
}
}

基于SSM的单点登陆05的更多相关文章

  1. 基于SSM的单点登陆04

    jdbc.properties JDBC_DRIVER=org.mariadb.jdbc.Driver JDBC_URL=jdbc:mariadb://127.0.0.1:3306/market JD ...

  2. 基于SSM的单点登陆01

    使用SSM的Maven聚合项目 建立父项目market的pom文件 <?xml version="1.0" encoding="UTF-8"?> & ...

  3. 基于SSM的单点登陆03

    TbUser.java和TbUserExample.java,TbUserMapper.java,TbUserMapper.xml由mybatis框架生成. generatorConfig.xml & ...

  4. 基于SSM的单点登陆02

    pom文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http: ...

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

    Spring Security 解析(六) -- 基于JWT的单点登陆(SSO)开发及原理解析   在学习Spring Cloud 时,遇到了授权服务oauth 相关内容时,总是一知半解,因此决定先把 ...

  6. 集成基于OAuth协议的单点登陆

    在之前的一篇文章中,我们已经介绍了如何为一个应用添加对CAS协议的支持,进而使得我们的应用可以与所有基于CAS协议的单点登陆服务通讯.但是现在的单点登陆服务实际上并不全是通过实现CAS协议来完成的.例 ...

  7. 集成基于CAS协议的单点登陆

    相信大家对单点登陆(SSO,Single Sign On)这个名词并不感到陌生吧?简单地说,单点登陆允许多个应用使用同一个登陆服务.一旦一个用户登陆了一个支持单点登陆的应用,那么在进入其它使用同一单点 ...

  8. ASP.NET 单点登陆

    第一种:同主域但不同子域之间实现单点登陆 Form验证其实是基于身份cookie的验证.客户登陆后,生成一个包含用户身份信息(包含一个ticket)的cookie,这个cookie的名字就是在web. ...

  9. ASP.NET在不同情况下实现单点登陆(SSO)的方法

    第一种:同主域但不同子域之间实现单点登陆 Form验证其实是基于身份cookie的验证.客户登陆后,生成一个包含用户身份信息(包含一个ticket)的cookie,这个cookie的名字就是在web. ...

随机推荐

  1. 用Java自定义一个定时器

    1.先定义一个监听类: import java.util.Date; import java.util.Timer; import javax.servlet.ServletContextEvent; ...

  2. window 计算机 开启事务

    window 操作系统如何开启事务 c#开发中使用事务调试程序的时候必须开启本地计算机的事务,如何开启呢: 1:控制面板 2:组件服务 3:本地DTC 4:设置 5:应用成功.

  3. ionicframework I ------------- 初体验

    ionicframework I -------------  初体验 Create hybrid mobile apps with the web technologies you love. Fr ...

  4. C#中对文件的操作小结

    1.建立一个文本文件 public class FileClass { public static void Main() { WriteToFile(); } static void WriteTo ...

  5. IO流入门-第十二章-ObjectInputStream_ObjectOutputStream

    DataInputStream和DataOutputStream基本用法和方法示例,序列化和反序列化 import java.io.Serializable; //该接口是一个“可序列化”的 ,没有任 ...

  6. sqlalchemy笔记

    http://jzqt.github.io/2015/12/29/SQLAlchemy%E7%AC%94%E8%AE%B0/ 用SQLAlchemy做ORM也有一段时间了,总结一下用过的一些地方. 连 ...

  7. Python图像处理库Pillow入门

    http://python.jobbole.com/84956/ Pillow是Python里的图像处理库(PIL:Python Image Library),提供了了广泛的文件格式支持,强大的图像处 ...

  8. 对protected函数的简单理解 good

    对于protected提供的函数和属性,除非想扩充这个类的功能,否则是完全用不到的.外部函数main()永远只能调用public的那些函数.所以从拖拉控件编程的角度来讲,只需要学习public的函数和 ...

  9. 通过文件对照工具Merge数据库

    项目分成线下开发版.线上測试版.线上生产版,因此相应有三个数据库. 对于一些静态数据.经常须要同步.改动了线下的开发版本号,同一时候也须要更新线上的測试版和线上生产版数据库,有时候线上的一些数据库改动 ...

  10. Python-selenium 下拉框定位

    1.通过select 进行定位下拉框 首先selenium 很人性化的给提供了一个Select的模块,供处理下来菜单,首先我们需要导入Select,通过from selenium.webdriver. ...