Spring Boot集成okta

Spring Boot集成okta


前言

最近在工作中接触到了okta,并与Spring Boot进行集成,积累了一点点经验,分享出来给各位同行参考一下。hhh


一、okta是什么?

简单来说就是一个企业统一认证平台。

官网地址:The World’s #1 Identity Platform | OktaOkta is the #1 trusted platform to secure every identity, from customers to your workforce with SSO, Multi-factor Authentication, Lifecycle Management, and more.https://www.okta.com/

二、使用步骤

1.准备工作

  • 注册一个okta账号,okta注册地址
  • 创建一个Application,create an okta integration for your app
  • 获取该Application的clientIdclientSecretdomain,如下:
clientId:0oa4yo3mr6chFXfKO5d7
clientSecret:YPcqGW30JbW04LSfvvLYJc_DIdEuaK5I7VyNnMPs
domain:dev-86782088.okta.com

2.开发工作

a、pom.xml中引入okta相关依赖

com.okta.springokta-spring-boot-starter1.4.0

com.okta.springokta-spring-sdk1.4.0

注意: 因为我的Spring Boot 版本为2.3.12.RELEASE,参考https://start.spring.io/actuator/infoOKTA,okta的version选择1.4.0

b、application.properties加入okta相关配置:

okta.oauth2.issuer=https://dev-86782088.okta.com/oauth2/default
okta.oauth2.client-id=0oa4yo3mr6chFXfKO5d7
okta.oauth2.client-secret=YPcqGW30JbW04LSfvvLYJc_DIdEuaK5I7VyNnMPs
okta.oauth2.redirect-uri=/authorization-code/callback

c、主启动类Application.java

@SpringBootApplication
@Slf4j
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}@Configuration@EnableWebSecuritystatic class OktaOAuth2WebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().anyRequest().authenticated().and().oauth2Client().and().oauth2Login().and().oauth2ResourceServer().jwt();}}
}

d、控制器Controller.java

@RestController
public class TestController {@GetMapping("/hello")public String logout(@AuthenticationPrincipal OidcUser oidcUser) {return "Hello: " + oidcUser.getFullName();}}

e、访问localhost:8080/hello

然后输入自己的用户名密码:

 成功访问页面,nice~


总结

        以上就是Spring Boot集成okta的相关内容,如果大家有遇到相关的问题,可以在下面留言哦,我尽力帮大家解决。


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部