释意
Single Sign On,一次登录,处处穿梭(类比Java,一次编写,多平台运行)
粗略的实例
凭借学校的一卡通,可以自由出入图书室、教学楼、行政楼等,办理自己的业务
详细的实例
购买景区通票
1、掏钱买门票(凭借用户名密码登录系统首页)
2、检票员检验有没有门票、门票是否过期(子系统验证使用者是否有cookie,而且是有效的cookie)
3、如果没有,购票进入(重新登录);如果有,放行(跳转到子系统)。
本质
服务器做了两件事,服务器对浏览做了存储cookie,服务器对每一个子系统做了是否需要验证cookie才可以被访问。
代码实现
1、服务器存储Cookie
@RequestMapping("/login")
public ModelAndView login() {
return new ModelAndView("index/login");
}
@PostMapping("/check")
public ModelAndView checkLogin(
HttpServletRequest request,
HttpServletResponse response,
@RequestParam(value = "username") String username,
@RequestParam(value = "password") String password,
@RequestParam(value = "gotoUrl", defaultValue = "/list") String gotoUrl) {
if (LoginUtil.check(username, password)) {
CookieUtil.set(response, "ssocookie", "genesso", 7200);
return new ModelAndView("redirect:" + gotoUrl);
}
return new ModelAndView("index/login");
}
2、服务器校验Cookie
@GetMapping("/list")
public ModelAndView list(
HttpServletRequest request,
HttpServletResponse response,
@RequestParam(value = "page", defaultValue = "1") Integer page,
@RequestParam(value = "size", defaultValue = "10") Integer size,
Map<String, Object> map) {
if ( ! CookieUtil.check(request)) {
map.put("gotoUrl", "/list");
return new ModelAndView("index/login", map);
}
PageRequest pageRequest = PageRequest.of(page - 1, size);
Page<FileMdata> fileMdataPage = fileMdataService.findList(pageRequest);
if (fileMdataPage == null) {
map.put("msg", ResponseCode.DATABASE_EMPTY.getMsg());
map.put("url", "/list");
return new ModelAndView("common/error", map);
}
map.put("fileMdataPage", fileMdataPage);
map.put("currentPage", page);
map.put("size", size);
return new ModelAndView("file/list", map);
}