该篇文章基于cookie实现,局限性很大,仅供学习,实际项目不要这么用。
1 首先新建项目一,项目结构如下图
2 接着将该项目端口设为8081;
3 接着编写一个项目1主页
4 接着编写主页控制器,代码如下
package com.chenyulin.controller;
import com.chenyulin.util.SSOCheck;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
/**
* Created by Administrator on 2017/6/29.
*/
@Controller
public class Demo1Controller {
private String gotoUrl;
@GetMapping("/demo1")
public ModelAndView main(HttpServletRequest request){
if (SSOCheck.checkCookie(request)) {
ModelAndView m1 = new ModelAndView("Demo1");
return m1;
}
//failed to login add "gotoUrl"
gotoUrl = "/demo1";
ModelAndView m2 = new ModelAndView("redirect:http://127.0.0.1:8080/intoLogin");
m2.addObject("gotoUrl", gotoUrl);
return m2;
}
}
工具类代码如下
package com.chenyulin.util;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
/**
* Created by Administrator on 2017/6/29.
*/
public class SSOCheck {
public static boolean checkCookie(HttpServletRequest request) {
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("ssocookie")&&cookie.getValue().equals("sso")) {
return true;
}
}
}
return false;
}
}
5 然后是认证中心
控制器代码
package com.chenyulin.controller;
import com.chenyulin.util.SSOCheck;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.ModelAndViewDefiningException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
/**
* Created by Administrator on 2017/6/29.
*/
@Controller
public class LoginController {
@GetMapping("/intoLogin")
private ModelAndView intoLogin(@RequestParam String gotoUrl) {
ModelAndView m=new ModelAndView("login");
m.addObject("gotoUrl", gotoUrl);
return m;
}
@PostMapping("login")
private String doLogin(@RequestParam String username, @RequestParam String password, @RequestParam String gotoUrl, HttpServletResponse response) {
boolean ok = SSOCheck.checkLogin(username, password);
if (ok) {
Cookie cookie = new Cookie("ssocookie", "sso");
cookie.setPath("/");
response.addCookie(cookie);
return "redirect:http://127.0.0.1:8081"+gotoUrl;
}
return null;
}
}
工具类代码
package com.chenyulin.util;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
/**
* Created by Administrator on 2017/6/29.
*/
public class SSOCheck {
private static final String USERNAME = "user";
private static final String PASSWORD = "123";
public static boolean checkLogin(String username, String password) {
if (username != null && password != null) {
if (username.equals(USERNAME) && password.equals(PASSWORD)) {
return true;
}
}
return false;
}
}
6 项目2,将项目2的端口改为8082,其它如同项目一,简单,略去
同时启动两项目和认证中心,输入:127.0.0.1:8081/demo1启动进入项目1登录页;127.0.0.1:8082/demo2启动进入项目2登录页;任意登录一个即可实现来回穿梭。
下面附上源码下载地址:http://download.csdn.net/detail/qwqw3333333/9884753
本人初涉,如有不足请指正,万分感谢qq:569265915