最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Springboot繼承Keycloak實(shí)現(xiàn)單點(diǎn)登錄與退出功能

 更新時(shí)間:2023年08月29日 09:51:35   作者:xxb249  
這篇文章主要介紹了Springboot繼承Keycloak實(shí)現(xiàn)單點(diǎn)登陸與退出,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

什么是 keycloak

keycloak是一個(gè)開源的進(jìn)行身份認(rèn)證和訪問控制的軟件。是由Red Hat基金會(huì)開發(fā)的,我們可以使用keycloak方便的向應(yīng)用程序和安全服務(wù)添加身份認(rèn)證,非常的方便。基于 Java 開發(fā),支持多種數(shù)據(jù)庫。

由于網(wǎng)上博客大部分都只有登陸沒有退出,自己花了一些時(shí)間研究了一下,這里將相關(guān)內(nèi)容進(jìn)行記錄,基于Keyclaok 20的版本,實(shí)現(xiàn)springboot服務(wù)單點(diǎn)登錄與退出

一、依賴

<!-- 在父工程中 -->
<dependencyManagement>
    <dependencies>
        <!-- 導(dǎo)入依賴 -->
        <dependency>
            <groupId>org.keycloak.bom</groupId>
            <artifactId>keycloak-adapter-bom</artifactId>
            <version>22.0.1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<!-- 在子工程中 -->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.keycloak</groupId>
        <artifactId>keycloak-spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.keycloak</groupId>
        <artifactId>keycloak-spring-security-adapter</artifactId>
    </dependency>
</dependencies>

二、keycloak配置

這個(gè)是主要的,用設(shè)置攔截器實(shí)現(xiàn)登陸與退出

package com.example.basic.conf;
import org.keycloak.KeycloakPrincipal;
import org.keycloak.KeycloakSecurityContext;
import org.keycloak.adapters.springboot.KeycloakSpringBootConfigResolver;
import org.keycloak.adapters.springsecurity.KeycloakConfiguration;
import org.keycloak.adapters.springsecurity.account.SimpleKeycloakAccount;
import org.keycloak.adapters.springsecurity.authentication.KeycloakAuthenticationProvider;
import org.keycloak.adapters.springsecurity.config.KeycloakWebSecurityConfigurerAdapter;
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.mapping.SimpleAuthorityMapper;
import org.springframework.security.core.session.SessionRegistryImpl;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.security.web.authentication.session.RegisterSessionAuthenticationStrategy;
import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@KeycloakConfiguration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class KeycloakSecurityConfiguration extends KeycloakWebSecurityConfigurerAdapter {
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider();
        keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(new SimpleAuthorityMapper());
        auth.authenticationProvider(keycloakAuthenticationProvider);
    }
    @Bean
    @Override
    protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
        return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
    }
    @Bean
    public org.keycloak.adapters.KeycloakConfigResolver KeycloakConfigResolver() {
        return new KeycloakSpringBootConfigResolver();
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
        http
            .logout()
                 //攔截logout請求
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                .addLogoutHandler(keycloakLogoutHandler())
                .logoutSuccessHandler(logoutSuccessHandler())
                .deleteCookies("JSESSIONID")
            .and()
             //設(shè)置哪些可以忽略掉授權(quán)
            .authorizeRequests()
                .antMatchers("/user/login", "/token/generate",
                    "/access/**", "/js/**","/css/**","/fonts/**", "/index.html", "/error").permitAll()
                 //除了上面忽略掉授權(quán)請求,剩下所有必須經(jīng)過授權(quán)才可以訪問
                .antMatchers("/**").authenticated()
            .and().cors()
            .and().csrf().disable();
    }
    //處理logout自動(dòng)跳轉(zhuǎn)請求
    private LogoutSuccessHandler logoutSuccessHandler() {
        return new LogoutSuccessHandler() {
            @Override
            public void onLogoutSuccess(HttpServletRequest httpServletRequest,
                    HttpServletResponse httpServletResponse, Authentication authentication)
                    throws IOException, ServletException {
                KeycloakAuthenticationToken keycloakAuthenticationToken = (KeycloakAuthenticationToken)authentication;
                KeycloakSecurityContext keycloakSecurityContext =
                        keycloakAuthenticationToken.getAccount().getKeycloakSecurityContext();
                String idTokenHint = keycloakSecurityContext.getIdTokenString();
                String issuer = keycloakSecurityContext.getIdToken().getIssuer();
                String keycloakBaseUrl = issuer + "/protocol/openid-connect/logout";
                String postLogoutRedirectUri = httpServletRequest.getScheme() + "://" + httpServletRequest.getHeader("host");
                String logoutUrl = keycloakBaseUrl + "?post_logout_redirect_uri=" + postLogoutRedirectUri + "&id_token_hint=" + idTokenHint;
                // Do logout by redirecting to Keycloak logout
                httpServletResponse.sendRedirect(logoutUrl);
            }
        };
    }
}

到此這篇關(guān)于Springboot繼承Keycloak實(shí)現(xiàn)單點(diǎn)登錄與退出功能的文章就介紹到這了,更多相關(guān)Springboot Keycloak單點(diǎn)登錄與退出內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 在SpringBoot項(xiàng)目中使用JetCache緩存的詳細(xì)教程

    在SpringBoot項(xiàng)目中使用JetCache緩存的詳細(xì)教程

    Spring Boot是一個(gè)非常流行的Java開發(fā)框架,JetCache是一個(gè)基于注解的高性能緩存框架,本文將介紹如何在Spring Boot項(xiàng)目中使用JetCache緩存,并提供一個(gè)詳細(xì)案例來說明如何配置和使用JetCache,需要的朋友可以參考下
    2024-06-06
  • 利用java模擬實(shí)現(xiàn)鍵盤鼠標(biāo)操作(附源碼)

    利用java模擬實(shí)現(xiàn)鍵盤鼠標(biāo)操作(附源碼)

    這篇文章主要為大家詳細(xì)介紹了如何從零設(shè)計(jì)并實(shí)現(xiàn)一個(gè)功能完備的鍵盤鼠標(biāo)模擬庫,提供比原生?Robot?更友好的?API,更高的可定制性和可擴(kuò)展性,感興趣的小伙伴可以了解一下
    2025-05-05
  • Java利用Jackson序列化實(shí)現(xiàn)數(shù)據(jù)脫敏

    Java利用Jackson序列化實(shí)現(xiàn)數(shù)據(jù)脫敏

    這篇文章主要介紹了利用Jackson序列化實(shí)現(xiàn)數(shù)據(jù)脫敏,首先在需要進(jìn)行脫敏的VO字段上面標(biāo)注相關(guān)脫敏注解,具體實(shí)例代碼文中給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2021-10-10
  • Java并發(fā)編程之閉鎖與柵欄的實(shí)現(xiàn)

    Java并發(fā)編程之閉鎖與柵欄的實(shí)現(xiàn)

    這篇文章主要介紹了Java并發(fā)編程之閉鎖與柵欄的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Swing常用組件之單選按鈕和復(fù)選框

    Swing常用組件之單選按鈕和復(fù)選框

    Swing是一個(gè)用于開發(fā)Java應(yīng)用程序用戶界面的開發(fā)工具包,這篇文章主要介紹了Swing常用組件之單選按鈕和復(fù)選框,感興趣的朋友可以參考一下
    2016-05-05
  • java開發(fā)分布式服務(wù)框架Dubbo調(diào)用過程

    java開發(fā)分布式服務(wù)框架Dubbo調(diào)用過程

    這篇文章主要為大家介紹了java開發(fā)分布式服務(wù)框架Dubbo調(diào)用過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2021-11-11
  • JAVA簡單實(shí)現(xiàn)MD5注冊登錄加密實(shí)例代碼

    JAVA簡單實(shí)現(xiàn)MD5注冊登錄加密實(shí)例代碼

    本篇文章主要介紹了JAVA簡單實(shí)現(xiàn)MD5注冊登錄加密實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-03-03
  • SpringBoot中使用MyBatis-Plus詳細(xì)步驟

    SpringBoot中使用MyBatis-Plus詳細(xì)步驟

    MyBatis-Plus是MyBatis的增強(qiáng)工具,簡化了MyBatis的使用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2025-01-01
  • 詳解Java實(shí)現(xiàn)單例的五種方式

    詳解Java實(shí)現(xiàn)單例的五種方式

    這篇文章主要介紹了詳解Java實(shí)現(xiàn)單例的五種方式,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-07-07
  • Springboot?+redis+谷歌開源Kaptcha實(shí)現(xiàn)圖片驗(yàn)證碼功能

    Springboot?+redis+谷歌開源Kaptcha實(shí)現(xiàn)圖片驗(yàn)證碼功能

    這篇文章主要介紹了Springboot?+redis+?歌開源Kaptcha實(shí)現(xiàn)圖片驗(yàn)證碼功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-01-01

最新評論

焦作市| 梨树县| 兴隆县| 永善县| 肥西县| 舟曲县| 汉川市| 留坝县| 红安县| 平南县| 玛沁县| 富源县| 柏乡县| 顺义区| 琼结县| 嵩明县| 县级市| 乐昌市| 南澳县| 安吉县| 五大连池市| 宜君县| 农安县| 惠水县| 井冈山市| 尉氏县| 英吉沙县| 荣昌县| 周至县| 页游| 镇坪县| 屯昌县| 英吉沙县| 梁山县| 庆城县| 灵璧县| 合山市| 大宁县| 调兵山市| 伊川县| 兴国县|