JAVA-SDK集成
# 开始
提示
主服务地址:http://10.6.0.108 后端地址http://10.6.0.108/api
# 1、后端jssdk集成
因为前后端不分离的情况下,请求都是先经后端跳转到前端页面的,所以前端集成jssdk的作用优先级很小了,首先后端拦截器就给拦截了所以采用后端接入
后端接入sdk,原理是通过filter过滤器实现的,后端接入需要保证sdk中的拦截器优先执行,也就意味着你的过滤器等级要大于1即可,sdk中封装 好了直接请求后端认证的接口,认证完成之后,会将认证之后的信息保存到session和上下文当中,你的拦截器只需要从上下文中获取就行了。
# 1、首先依赖pom文件
版本号以最新发布的为主
<dependency>
<groupId>com.scsoft</groupId>
<artifactId>auth-sso-spring-boot-starter</artifactId>
<version>2.2.1</version>
</dependency>
2
3
4
5
# 2、在启动类或者配置类上面添加注解@EnableAuthSsoClient
@SpringBootApplication
@EnableAuthSsoClient
public class XXXApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
}
}
//或者专门有个配置类加入注解
@Configuration
@EnableAuthSsoClient
public class SsoConfig implements WebMvcConfigurer {
private static Logger logger = LoggerFactory.getLogger(CasConfig.class);
}
2
3
4
5
6
7
8
9
10
11
12
13
# 3、将oauth.properties配置文件拷贝到resource目录下
#单点登录认证方式 0基于cookies 1前后端分离模式
auth.sso.type=0
#拦截哪些地址进行token校验
auth.sso.path=/*
#排除拦截的地址
auth.sso.exclude-path=/sso/logout,/error/**,/sinuo/**
#认证中心的地址
auth.sso.auth-center-url=http://127.0.0.1:9001/auth/sso/ssoLogin
#认证中心退出地址
auth.sso.auth-logout-url=http://127.0.0.1:9000/auth/sso/ssoLogin
2
3
4
5
6
7
8
9
10
11
# 4、将以下拦截器代码拷贝到项目中去
拦截器里面的逻辑要根据自己的认证机制去更改,只要从request里面能拿到用户信息,就认为单点已经通过了 这个只是我提供的一个示例,并不是真实代码,需要根据自己的业务实现,可以是其他方式实现的不一定非是这样
package com.scsoft.scpt.common.filter;
import com.alibaba.fastjson.JSONObject;
import com.scsoft.scpt.exception.BusinessException;
import com.scsoft.scpt.system.entity.User;
import com.scsoft.scpt.system.service.IUserService;
import com.scsoft.scpt.tokenfactory.AuthToken;
import com.scsoft.scpt.tokenfactory.AuthTokenDao;
import com.scsoft.scpt.utils.StringUtils;
import com.scsoft.scpt.utils.WebUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.annotation.Resource;
import javax.servlet.FilterChain;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 如果校验通过,就认为这是一个取得授权的合法请求
* @Author: zhaopengfei
* @copyright: 雪城软件有限公司
* All rights Reserved,Designed By Scsoft
* @CreateDate: 2022/9/21 16:05
* @Version: 1.0
*/
//@Component
@Slf4j
public class SinuoSsoTokenFilter extends OncePerRequestFilter {
@Resource
private AuthTokenDao authTokenDao;
@Resource
private IUserService userService;
@Override
public void initFilterBean() {
ServletContext servletContext = this.getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
userService = ctx.getBean(IUserService.class);
authTokenDao = ctx.getBean(AuthTokenDao.class);
}
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response,
FilterChain chain) throws ServletException, IOException {
System.out.println("请求地址:"+request.getRequestURL());
String currentName = (String) request.getAttribute("currentName");
if (StringUtils.isBlank(currentName)){
currentName= (String) request.getSession().getAttribute("currentName");
}
if (StringUtils.isNotBlank(currentName) && StringUtils.isBlank(request.getHeader("Authorization"))){
User user = userService.getByUsername(currentName);
if (user == null) {
throw new BusinessException(HttpStatus.UNAUTHORIZED.value(), "用户信息不存在");
}
AuthToken authToken = authTokenDao.createToken(user.getId());
System.out.println("token:" + authToken.getAccessToken());
// 存放json对象
authToken.setPrincipals(user);
authTokenDao.updatePrincipalsByUserId(user.getId(),user);
// 此处放在request中,方便后面再/sso/getTokenByTicket接口中使用
request.getSession().setAttribute("sinuo_xx", authToken.getAccessToken());
request.setAttribute("sinuo_xx", authToken.getAccessToken());
System.out.println("filter_token: " + authToken.getAccessToken());
}
chain.doFilter(request, response);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
核心代码就几句就行 currentUser是获取对象的,currentName是获取名称的,实际上上下文放的都有
String currentName = (String) request.getAttribute("currentName");
if (StringUtils.isBlank(currentName)){
currentName= (String) request.getSession().getAttribute("currentName");
}
2
3
4
5
# 5、将拦截器注册到bean中去
如果你采用@webservlet注解就不需要这个了,这个只是把拦截器注册到bean中,上面方法加@webservlet就自动注册了 当然如果不采用过滤器,采用intercept的话,也试试可以的,不是必须使用filter的,主要还是根据你的需求改造
import com.scsoft.scpt.filter.SinuoSsoTokenFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @title: CasConfig
* @Description:
* @Author: zhaopengfei
* @CreateDate: 2023/10/10 14:12
* @Version: 1.0*/
@Configuration
public class SsoConfig implements WebMvcConfigurer {
private static Logger logger = LoggerFactory.getLogger(SsoConfig.class);
/**
* 配置授权过滤器
* 单点登录认证
* @return
*/
@Bean
public FilterRegistrationBean filterRegistration() {
final FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new SinuoSsoTokenFilter());
// 设定加载的顺序
registration.setOrder(1);
return registration;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 6、如果不想使用这些 也可以直接自己调用获取用户信息的接口
请求地址 http://ip:port/getUserByAuthCode?authCode=xx
参数
| 属性 | 值 | 描述 |
|---|---|---|
| authCode | string | 拿到的authcode值,ticket |
返回值 code:200 message:成功 data:{ userId:xxx, userName:xxx, realName:xxx }
| 属性 | 值 | 描述 |
|---|---|---|
| userId | string | XXX |
| userName | string | XXX |
| realName | string | XXX |
JAVASDK下载:
因为我们jar包在自己私服上面,暂时无法开放内网,于是提供离线下载方式,你可以将该包添加到自己的私服仓库中,也可以将该包加入到本地仓库也可以
离线下载:
因为我sdk包本身依赖其他第三方的库,这些库都是常见的一些包,大部分都会用到,如果你项目中已经使用到就无需再额外引入,如果没有用到 需要将下面包先引入
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
</dependencies>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
以上版本不做过多要求,如果某些jar包 你项目中引用了,可以尝试用你项目中的jar包,只要不冲突即可,
本sdk依赖springboot底层,如果你项目暂时没有使用springboot,可自行实现 无需引用本sdk,具体实现方式可以参考自行接入