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.准备工作

clientId:0oa4yo3mr6chFXfKO5d7
clientSecret:YPcqGW30JbW04LSfvvLYJc_DIdEuaK5I7VyNnMPs
domain:dev-86782088.okta.com

2.开发工作

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

<dependency>
    <groupId>com.okta.spring</groupId>
    <artifactId>okta-spring-boot-starter</artifactId>
    <version>1.4.0</version>
</dependency>
<dependency>
    <groupId>com.okta.spring</groupId>
    <artifactId>okta-spring-sdk</artifactId>
    <version>1.4.0</version>
</dependency>

注意: 因为我的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
    @EnableWebSecurity
    static class OktaOAuth2WebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

        @Override
        protected 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的相关内容,如果大家有遇到相关的问题,可以在下面留言哦,我尽力帮大家解决。

评论 39
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JonTang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值