基于SSM的单点登陆05

Posted guanghe

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于SSM的单点登陆05相关的知识,希望对你有一定的参考价值。

技术分享图片

springmvc.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:mvc="http://www.springframework.org/schema/mvc"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
 8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 9         <!-- 解析properties文件的工具类 -->
10         <context:property-placeholder location="classpath:*.properties"/>
11 
12         <!-- 扫描controller组件 -->
13         <context:component-scan base-package="io.guangsoft.market.controller"/>
14 
15         <!-- 开启注解驱动 -->
16         <mvc:annotation-driven />
17         
18         <!-- 配置视图解析 -->
19         <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
20             <property name="prefix" value="/jsp/" />
21             <property name="suffix" value=".jsp" />
22         </bean>
23         <!-- 资源映射 -->
24         <mvc:resources location="/css/" mapping="/css/**"/>
25         <mvc:resources location="/js/" mapping="/js/**"/>
26         <mvc:resources location="/images/" mapping="/images/**"/>
27  </beans>

web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.5" 
 3     xmlns="http://java.sun.com/xml/ns/javaee" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 7     <!-- 启动spring -->
 8     <context-param>
 9         <param-name>contextConfigLocation</param-name>
10         <param-value>classpath:spring-*.xml</param-value>
11     </context-param>
12     <listener>
13         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
14     </listener>
15     
16     <!-- 解决post乱码 -->
17     <filter>
18         <filter-name>CharacterEncodingFilter</filter-name>
19         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
20         <init-param>
21             <param-name>encoding</param-name>
22             <param-value>utf-8</param-value>
23         </init-param>
24     </filter>
25     <filter-mapping>
26         <filter-name>CharacterEncodingFilter</filter-name>
27         <url-pattern>/*</url-pattern>
28     </filter-mapping>
29     
30     <!-- 启动springmvc -->
31     <servlet>
32         <servlet-name>market</servlet-name>
33         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
34         <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
35         <init-param>
36             <param-name>contextConfigLocation</param-name>
37             <param-value>classpath:springmvc.xml</param-value>
38         </init-param>
39         <load-on-startup>1</load-on-startup>
40     </servlet>
41     <servlet-mapping>
42         <servlet-name>market</servlet-name>
43         <url-pattern>/</url-pattern>
44     </servlet-mapping>
45     
46     <welcome-file-list>
47         <welcome-file>index.html</welcome-file>
48         <welcome-file>index.htm</welcome-file>
49         <welcome-file>index.jsp</welcome-file>
50         <welcome-file>default.html</welcome-file>
51         <welcome-file>default.htm</welcome-file>
52         <welcome-file>default.jsp</welcome-file>
53     </welcome-file-list>
54 </web-app>

PageController.java

 1 package io.guangsoft.market.controller;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.ui.Model;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 
 7 /**
 8  * 页面跳转
 9  */
10 @Controller
11 public class PageController {
12 
13     @RequestMapping("/login")
14     public String showLogin(String redirect, Model model) {
15         model.addAttribute("redirect", redirect);
16         return "login";
17     }
18 
19     @RequestMapping("/register")
20     public String showRegister() {
21         return "register";
22     }
23 }

UserController.java

  1 package io.guangsoft.market.controller;
  2 
  3 import javax.servlet.http.HttpServletRequest;
  4 import javax.servlet.http.HttpServletResponse;
  5 
  6 import io.guangsoft.market.util.bean.GResult;
  7 import io.guangsoft.market.dao.bean.TbUser;
  8 import io.guangsoft.market.service.UserService;
  9 import io.guangsoft.market.util.utils.CookieUtil;
 10 import io.guangsoft.market.util.utils.GResultUtil;
 11 import org.apache.commons.lang3.StringUtils;
 12 import org.springframework.beans.factory.annotation.Autowired;
 13 import org.springframework.beans.factory.annotation.Value;
 14 import org.springframework.http.converter.json.MappingJacksonValue;
 15 import org.springframework.stereotype.Controller;
 16 import org.springframework.web.bind.annotation.PathVariable;
 17 import org.springframework.web.bind.annotation.RequestMapping;
 18 import org.springframework.web.bind.annotation.RequestMethod;
 19 import org.springframework.web.bind.annotation.ResponseBody;
 20 
 21 
 22 /**
 23  * 用户登录服务controller
 24  */
 25 @Controller
 26 @RequestMapping("/user")
 27 public class UserController {
 28 
 29     @Autowired
 30     private UserService userService;
 31 
 32     @Value("${REDIS_USER_SESSION_KEY}")
 33     private String cookieName;
 34     
 35     /**
 36      * 用户注册数据校验接口
 37      */
 38     @RequestMapping(value="/check/{param}/{type}",method=RequestMethod.GET)
 39     @ResponseBody
 40     public Object checkData(@PathVariable String param,@PathVariable int type,String callback){
 41         GResult result = null;
 42         //数据校验
 43         if(type != 1 && type != 2 && type != 3){
 44            result = GResultUtil.fail(-1, "参数类型有误!");
 45         }else{
 46         //调用业务层做数据校验
 47            result = this.userService.userParamCheck(param, type);
 48         }
 49         //判断是否使用jsonp调用方式
 50         if(callback != null && callback.length() > 0){
 51             MappingJacksonValue mapping = new MappingJacksonValue(result);
 52             mapping.setJsonpFunction(callback);
 53             return mapping;
 54         }
 55         return result;
 56     }
 57     
 58     /**
 59      * 用户注册接口
 60      */
 61     @RequestMapping(value="/register",method=RequestMethod.POST)
 62     @ResponseBody
 63     public GResult userRegister(TbUser user){
 64         try{
 65             return this.userService.userRegister(user);
 66         }catch(Exception e){
 67             e.printStackTrace();
 68             return GResultUtil.fail(-1, "注册失败. 请校验数据后请再提交数据!");
 69         }
 70     }
 71     
 72     /**
 73      * 用户登录接口
 74      */
 75     @RequestMapping(value="/login",method=RequestMethod.POST)
 76     @ResponseBody
 77     public GResult userLogin(String username,String password,HttpServletRequest request,HttpServletResponse repsonse){
 78         try{
 79             GResult result = this.userService.userLogin(username, password);
 80             if(result.getgCode() == 0) {
 81                 CookieUtil.setCookie(request, repsonse, cookieName, result.getgData().toString());
 82             }
 83             return result;
 84         }catch(Exception e){
 85             e.printStackTrace();
 86             return GResultUtil.fail(-1,  e.getMessage());
 87         }
 88     }
 89     
 90     /**
 91      * 根据token查询用户
 92      */
 93     @RequestMapping(value="/token/{token}",method=RequestMethod.GET)
 94     @ResponseBody
 95     public Object findUserByToken(@PathVariable String token,String callback){
 96         GResult result = this.userService.findUserByToken(token);
 97         //判断是否含有jsonp
 98         if(!StringUtils.isBlank(callback)){
 99             MappingJacksonValue mapping = new MappingJacksonValue(result);
100             mapping.setJsonpFunction(callback);
101             return mapping;
102         }
103         return result;
104     }
105 }

 

以上是关于基于SSM的单点登陆05的主要内容,如果未能解决你的问题,请参考以下文章

基于SSM的单点登陆02

基于SSM的单点登陆04

SSM-MyBatis-05:Mybatis中别名,sql片段和模糊查询加getMapper

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

认证和SSO-基于OAuth2单点登陆基本架构

ssm拦截器的原理及实现