SpringBoot如何實現(xiàn)同域SSO(單點登錄)
單點登錄,其實看起來不是很復雜,只是細節(jié)上的處理,單點區(qū)分有三種
- 同域SSO
- 同父域SSO
- 跨域的SSO
如何實現(xiàn)同域SSO?
個人理解:當用戶登錄訪問demo1.lzmvlog.top時,同時具有訪問demo2.lzmvlog.top的能力,即認證完成一次,可以訪問所有系統(tǒng)。
實現(xiàn)方式:可以采用Cookie實現(xiàn),即用戶在訪問一個系統(tǒng)時,攜帶認證頒發(fā)的信息,系統(tǒng)響應是否具有訪問資格,否則跳轉(zhuǎn)認證,也可以采用Session,即Session共享,校驗訪問用戶是否具有有效的信息,提供訪問資格
代碼實現(xiàn)
依賴
<!--spring-data-jpa-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
配置
server:
port: 8090
spring:
application:
name: authority
datasource:
url: jdbc:mysql://127.0.0.1:3306/SSO?useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
當用戶訪問除登錄界面時,都需要提前認證,認證完成之后需要跳轉(zhuǎn)到之前訪問的路徑上,并提供訪問別的系統(tǒng)的權(quán)限。
實現(xiàn)邏輯,當用戶訪問任何路徑時,都需要通過攔截器的校驗,確認擁有訪問的權(quán)限,才能放行通過,不具有訪問權(quán)限的,重定向到 登錄界面,并保存原有訪問的頁面路徑,驗證成功的時候跳轉(zhuǎn)到原有頁面
控制器
@Controller
public class IndexController {
@Autowired
UserRepository userRepository;
/**
* 將要跳轉(zhuǎn)的路徑
*/
public String url;
/**
* 登錄界面
*
* @return
*/
@GetMapping("/index")
public String index() {
return "index";
}
/**
* 登錄界面
*
* @return
*/
@GetMapping("/")
public String index1() {
return "index";
}
/**
* 登錄請求接口
*
* @param username 賬號
* @param password 密碼
* @param response
* @return
*/
@PostMapping("login")
public String login(String username, String password, HttpServletResponse response) {
// 用戶登錄
boolean exists = userRepository.exists(Example.of(new User()
.setUsername(username)
.setPassword(password)));
if (exists) {
Cookie cookie = new Cookie("username", username);
response.addCookie(cookie);
// 如果正常訪問即跳轉(zhuǎn)到正常頁面
if (StringUtils.isEmpty(url)) {
return "demo1";
}
// 如果之前存在訪問的頁面,認證完成即跳轉(zhuǎn)會原有的頁面
return url;
}
return "index";
}
/**
* 跳轉(zhuǎn)到 demo2
*
* @return
*/
@GetMapping("demo2")
public String demo2() {
return "demo2";
}
/**
* 跳轉(zhuǎn)到 demo1
*
* @return
*/
@GetMappi=ng("demo1")
public String demo1() {
return "demo1";
}
}
攔截器實現(xiàn)
@Component
public class CookieHandlerInterceptor implements HandlerInterceptor {
@Autowired
UserRepository userRepository;
@Autowired
IndexController indexController;
/**
* 執(zhí)行方法之前
*
* @param request
* @param response
* @param handler
* @return
* @throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 獲取當前請求得路徑 如果不是正常得登錄界面請求 登錄成功之后需要跳轉(zhuǎn)到原來請求得界面上
String servletPath = request.getServletPath();
// 對不需要攔截得路徑進行放行
if ("/index".equals(servletPath) || "/".equals(servletPath) || "/login".equals(servletPath)) {
return true;
}
if (!"/index".equals(servletPath) || !"/".equals(servletPath)) {
indexController.url = servletPath;
}
Cookie[] cookies = request.getCookies();
boolean exists = false;
if (cookies != null) {
for (Cookie cookie : cookies) {
String value = cookie.getValue();
if (!StringUtils.isEmpty(value)) {
exists = userRepository.exists(Example.of(new User()
.setUsername(value)));
}
}
}
if (exists) {
return true;
} else {
response.sendRedirect("/index");
}
return false;
}
}
在SpringBoot2.x之后不能生效,需要將攔截器添加到攔截器鏈路中,即:
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
/**
* Session 攔截處理器
*/
@Autowired
private CookieHandlerInterceptor cookieHandlerInterceptor;
/**
* 添加攔截器
*
* @param registry
*/
@Override
protected void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(this.cookieHandlerInterceptor).addPathPatterns("/**");
super.addInterceptors(registry);
}
}
其實攔截器還有第二種實現(xiàn)方式,即通過Filter接口實現(xiàn)
@Component
public class CookieFilter extends OncePerRequestFilter {
@Autowired
UserRepository userRepository;
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
// 獲取當前請求得路徑 如果不是正常得登錄界面請求 登錄成功之后需要跳轉(zhuǎn)到原來請求得界面上
String servletPath = request.getServletPath();
IndexController indexController = new IndexController();
// 對不需要攔截得路徑進行放行
if ("/index".equals(servletPath) || "/".equals(servletPath) || "/login".equals(servletPath)) {
filterChain.doFilter(request, response);
}
if (!"/index".equals(servletPath) || !"/".equals(servletPath)) {
indexController.url = servletPath;
}
Cookie[] cookies = request.getCookies();
boolean exists = false;
if (cookies != null) {
for (Cookie cookie : cookies) {
String value = cookie.getValue();
if (!StringUtils.isEmpty(value)) {
exists = userRepository.exists(Example.of(new User()
.setUsername(value)));
}
}
}
if (exists) {
filterChain.doFilter(request, response);
} else {
response.sendRedirect("/");
}
}
}
其實也可以采用Session的方式實現(xiàn),采用共享Session的方式,我這里只是簡單的實現(xiàn)一下,其實在認證時可以結(jié)合SpringSecurity或者Shiro安全框架去整合JWT以保證信息的安全
界面
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登錄</title>
</head>
<body>
<div align="center">
<h1>請登錄</h1>
<form action="/login" method="post">
<span>賬號:</span><input name="username" type="text" value="zhang"><br>
<span>密碼:</span><input name="password" type="password" value="123456"><br>
<button type="submit" style="margin: 10px 0">登錄</button>
</form>
</div>
</body>
</html>
demo1.html和demo2.html只需要坐一下簡單的區(qū)分,知道是哪個頁面就行了
同域SSO其實不是很復雜,只是了解一下整個訪問的過程,和需要做的一些限制即可,后續(xù)看看做后面兩種的實現(xiàn)
即同父域SSO和跨域SSO
以上就是SpringBoot如何實現(xiàn)同域SSO(單點登錄)的詳細內(nèi)容,更多關于SpringBoot 實現(xiàn)同域SSO的資料請關注腳本之家其它相關文章!
相關文章
從application.properties配置文件獲取的漢字亂碼的解決方法
平時從配置文件各種讀取配置參數(shù)都正常,但是有時候放了個中文就亂碼,你肯定試過網(wǎng)上好多方法,都沒解決,那么來看下面,恭喜你終于找這里了,本文給大家介紹了從application.properties配置文件獲取的漢字亂碼的解決方法,需要的朋友可以參考下2024-03-03
mybatis resultType自帶數(shù)據(jù)類型別名解讀
MyBatis為了簡化開發(fā),通過org.apache.ibatis.type.TypeAliasRegistry為常見類定義了別名,這些別名包括基本數(shù)據(jù)類型及其數(shù)組、集合類型等,如string對應java.lang.String,int對應java.lang.Integer等,此外,還有特殊前綴的別名如_int對應int類型2024-10-10
SpringBoot可視化接口開發(fā)工具magic-api的簡單使用教程
作為Java后端開發(fā),平時開發(fā)API接口的時候經(jīng)常需要定義Controller、Service、Dao、Mapper、XML、VO等Java對象。有沒有什么辦法可以讓我們不寫這些代碼,直接操作數(shù)據(jù)庫生成API接口呢?今天給大家推薦一款工具magic-api,來幫我們實現(xiàn)這個小目標!2021-06-06
MyBatis-Plus條件構(gòu)造器Wrapper應用實例
QueryWrapper是用于查詢的Wrapper條件構(gòu)造器,可以通過它來構(gòu)建SELECT語句中的WHERE條件,這篇文章主要介紹了MyBatis-Plus數(shù)據(jù)表操作條件構(gòu)造器Wrapper,需要的朋友可以參考下2023-09-09
Spring Security單項目權(quán)限設計過程解析
這篇文章主要介紹了Spring Security單項目權(quán)限設計過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-11-11
ArrayList和LinkedList的區(qū)別、擴容機制以及底層的實現(xiàn)方式
這篇文章主要介紹了ArrayList和LinkedList的區(qū)別、擴容機制以及底層的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03

