Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve single sign-on problem with Springboot2.X

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

这期内容当中小编将会给大家带来有关Springboot2.X如何解决单点登陆,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

1、pom文件依赖 com.majiaxueyuan sso-core 1.2.22、sso配置文件

由于使用的是springboot2.2.0版本,所以配置需要实现WebMvcConfigurer

import com.majiaxueyuan.sso.core.filter.MaJiaSSOIntercepter;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;/** * @Author:MuJiuTian * @Description: 单点登陆配置WebMvcConfigurer * @Date: Created in 下午5:34 2019/10/21 */@Configurationpublic class SsoConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(ssoIntercepter()).addPathPatterns("/**"); } @Bean public MaJiaSSOIntercepter ssoIntercepter() { return new MaJiaSSOIntercepter().setTokenSalt("pwd_salt"); }}

如果是其他版本可以使用集成WebMvcConfigurerAdapter

import com.majiaxueyuan.sso.core.filter.MaJiaSSOIntercepter;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;/** * @Author:MuJiuTian * @Description: 单点登陆配置WebMvcConfigurerAdapter * @Date: Created in 下午5:34 2019/10/21 */@Configurationpublic class SsoConfig extends WebMvcConfigurerAdapter { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(ssoIntercepter()).addPathPatterns("/**"); } @Bean public MaJiaSSOIntercepter ssoIntercepter() { return new MaJiaSSOIntercepter().setTokenSalt("pwd_salt"); }}3、实体类@Data@AllArgsConstructor@NoArgsConstructorpublic class User { private int userId; private String slat; private String account; // ....其他的字段就不写了}4、service逻辑层/** * @Author:MuJiuTian * @Description: 简单测试,我就不写接口了,直接以class的形式 * @Date: Created in 下午5:47 2019/10/21 */@Servicepublic class LoginService { public User checkUser(String account, String pwd){ // 做假数据,加入密码登陆成功,返回user实体类 return new User(1,account,"ewdsbj"); }}5、controller层import com.majiaxueyuan.sso.core.annotation.NoToken;import com.majiaxueyuan.sso.core.constans.Result;import com.majiaxueyuan.sso.core.helper.TokenLoginHelper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;/** * @Author:MuJiuTian * @Description: 测试单点登录框架 * @Date: Created in 下午5:25 2019/10/21 */@RestControllerpublic class LoginController { @Autowired LoginService loginService; @GetMapping(value = "/testToken") public String testToken() { return "需要token"; } @GetMapping(value = "/login") @NoToken(notNeedToken = true) public String login(String account, String password) { // 测试用户输入的用户名、密码是否正确 User user = loginService.checkUser(account, password); if (user == null){ return "密码错误"; } // 如果密码成功后,获取用户主键id、以及注册时的salt Long user_id = Long.valueOf(user.getUserId()); String salt = user.getSlat(); // 然后通过sso-core框架获取token Result loginSuccess = TokenLoginHelper.loginSuccess(user_id,account,"",salt); // 获取token String token = ""; if (loginSuccess.getCode() == 200){ token = loginSuccess.getData().toString(); } return token; }}6、测试

测试接口1:

测试接口2:

7、获取request用户对象

用户获取到Token之后,返回给前端,前端携带时需要将Token添加到Header中的Authorization字段。当前强制使用此请求头字段,这些数据存储headers中带过来检测token是否过期或者登陆...情况。

SSOUser user = (SSOUser) request.getAttribute("ssoUser");8、@NoToken

当有某个请求不需要认证即可访问时,在此请求方法上添加注解@NoToken,不加注解需要验证,只要加上注解就表示不需要认证,不管@NoToken(notNeedToken = true)还是@NoToken(notNeedToken = false),都不需要验证,至少在目前最新1.2.2版本是这样。

上述就是小编为大家分享的Springboot2.X如何解决单点登陆了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report