欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot的Security和OAuth2的使用示例小結(jié)

 更新時間:2024年06月18日 08:51:03   作者:kiba518  
這篇文章主要介紹了SpringBoot的Security和OAuth2的使用,本文通過示例圖文相結(jié)合給大家講解的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧

創(chuàng)建項(xiàng)目

先創(chuàng)建一個spring項(xiàng)目。

然后編寫pom文件如下,引入spring-boot-starter-security,我這里使用的spring boot是2.4.2,這里使用使用spring-boot-dependencies,在這里就能找到對應(yīng)的security的包。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>app-kiba-security</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>app-kiba-security</name>
    <description>app-kiba-security</description>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.4.2</spring-boot.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <configuration>
                    <mainClass>com.kiba.appkibasecurity.AppKibaSecurityApplication</mainClass>
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

然后訪問創(chuàng)建項(xiàng)目時默認(rèn)生成的接口:http://127.0.0.1:8080/user/123/roles/222,得到如下界面。

image

這是相當(dāng)于,在我們的接口請求的前面做了一個攔截,類似filter,攔截后,跳轉(zhuǎn)到了一個界面,讓我們輸入賬號密碼。這里,我由于沒有設(shè)置賬號密碼,所以登錄不進(jìn)去。

設(shè)置訪問一

下面設(shè)置一個賬號密碼,并且設(shè)置hello接口可以直接訪問,設(shè)置很簡單,就是注入兩個bean,InMemoryUserDetailsManager和WebSecurityCustomizer,代碼如下:

@Configuration
public class SecurityConfig   {
    /**
     * 注冊用戶,這里用戶是在內(nèi)存中的
     *  {noop}表示“無操作”(No Operation)密碼編碼。
     * @return
     */
    @Bean
    UserDetailsService userDetailsService() {
        InMemoryUserDetailsManager users = new InMemoryUserDetailsManager();
        users.createUser(User.withUsername("kiba").password("{noop}123").roles("admin").build()); 
        return users;
    }
    /**
     * 讓hello可以不用登錄,就可以直接訪問,例如:http://127.0.0.1:8080/hello?name=kiba就可以直接訪問
     * @return
     */
    @Bean
    WebSecurityCustomizer webSecurityCustomizer() {
        return new WebSecurityCustomizer() {
            @Override
            public void customize(WebSecurity web) {
                web.ignoring().antMatchers("/hello");
            }
        };
    }
}

現(xiàn)在我們訪問http://127.0.0.1:8080/user/123/roles/222,進(jìn)入到登錄頁面,輸入kiba/123就可以查看接口執(zhí)行的結(jié)果了。

http://127.0.0.1:8080/hello?name=kiba就無需登錄,可以直接訪問。

登錄一次,其他接口就可以自由訪問了

控制請求

現(xiàn)在,增加一個類SecurityAdapter,繼承自WebSecurityConfigurerAdapter。然后重寫他的configure方法

@Configuration
@AllArgsConstructor
public class SecurityAdapter extends WebSecurityConfigurerAdapter {
    /**
     * authenticated():用戶需要通過用戶名/密碼登錄,記住我功能也可以(remember-me)。
     * fullyAuthenticated()用戶需要通過用戶名/密碼登錄,記住我功能不行。 
     */
    @Override
    @SneakyThrows
    protected void configure(HttpSecurity http) {
        http.httpBasic().and()
                //禁用跨站請求偽造(CSRF)保護(hù)。
                .csrf().disable()
                .authorizeRequests().anyRequest().fullyAuthenticated();
    } 
}

當(dāng)使用,增加了SecurityAdapter后,我們重新請求http://127.0.0.1:8080/user/123/roles/222,得到界面如下:

image

可以看到,登錄界面的樣式被美化了。

設(shè)置訪問二(推薦)

我們還可以使用第二種方法,來做用戶密碼的配置。

通過重寫configure(AuthenticationManagerBuilder auth)函數(shù),來創(chuàng)建用戶,這種方式創(chuàng)建用戶會將前面的bean-UserDetailsService給覆蓋,即,用戶只剩下這里創(chuàng)建的。

代碼如下:

@Configuration
@AllArgsConstructor
public class SecurityAdapter extends WebSecurityConfigurerAdapter {
    /**
     * authenticated():用戶需要通過用戶名/密碼登錄,記住我功能也可以(remember-me)。
     * fullyAuthenticated()用戶需要通過用戶名/密碼登錄,記住我功能不行。
     */
    @Override
    @SneakyThrows
    protected void configure(HttpSecurity http) {
        http.httpBasic().and()
                //禁用跨站請求偽造(CSRF)保護(hù)。
                .csrf().disable()
                .authorizeRequests().anyRequest().fullyAuthenticated();
    }
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("kiba518")
                .password(passwordEncoder().encode("123"))
                .authorities(new ArrayList<>(0));
    }
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

這里的用戶是寫死的,用戶是可以修改成讀取數(shù)據(jù)庫的信息的。

我們查看WebSecurityConfigurerAdapter的代碼,可以看到他有注解@Order(100),數(shù)越大,執(zhí)行越優(yōu)先級越低,即,他的執(zhí)行順序是相對比較靠后的。

授權(quán)OAuth2

授權(quán)這個設(shè)計(jì)理念是這樣,它是結(jié)合上面的security的操作,實(shí)現(xiàn)了一個普通的WebApp轉(zhuǎn)換成授權(quán)服務(wù)器WebApp。

授權(quán)服務(wù)器轉(zhuǎn)換思路

我們先了解一下security轉(zhuǎn)授權(quán)服務(wù)器的思路。

1,在這個應(yīng)用里,創(chuàng)建一個auth接口,然后任何人想訪問這個接口,就都需要輸入賬戶密碼了。

2,我們這個auth接口的返回值是個code,然后我們的前端,或者其他調(diào)用接口的APP,就可以把這個code作為用戶登錄的token了,。

3,然后我們再做一個接口,接受一個token參數(shù),可以驗(yàn)證token是否有效。

這樣我們這個授權(quán)服務(wù)器的搭建思路就構(gòu)建完成了。

但按這個思路,我們需要做很多操作,比如創(chuàng)建接口,緩存token等等,現(xiàn)在spring提供了一個Oauth2的包,他可以幫我們實(shí)現(xiàn)這些接口定義。

OAuth2的接口如下,可以自行研究。

/oauth/authorize:授權(quán)端點(diǎn)

/oauth/token:獲取令牌端點(diǎn)

/oauth/confirm_access:用戶確認(rèn)授權(quán)提交端點(diǎn)

/oauth/error:授權(quán)服務(wù)錯誤信息端點(diǎn)

/oauth/check_token:用于資源服務(wù)訪問的令牌解析端點(diǎn)

/oauth/token_key:提供公有密匙的端點(diǎn),如果使用JWT令牌的話

實(shí)現(xiàn)授權(quán)服務(wù)器

現(xiàn)在我們實(shí)現(xiàn)一個授權(quán)服務(wù)器。

先添加OAuth2的引用。

 <dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
            <version>2.4.0.RELEASE</version>
        </dependency>

然后增加配置文件AuthorizationConfig。

@Configuration
@EnableAuthorizationServer //開啟授權(quán)服務(wù)
public class AuthorizationConfig extends AuthorizationServerConfigurerAdapter {
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private AuthenticationManager authenticationManager;
    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        //允許表單提交
        security.allowFormAuthenticationForClients()
                .checkTokenAccess("isAuthenticated()");
    }
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                .withClient("client-kiba") //客戶端唯一標(biāo)識(client_id)
                .secret(passwordEncoder.encode("kiba518-123456")) //客戶端的密碼(client_secret),這里的密碼應(yīng)該是加密后的
                .authorizedGrantTypes("password") //授權(quán)模式標(biāo)識,共4種模式[授權(quán)碼(authorization-code)隱藏式(implicit) 密碼式(password)客戶端憑證(client credentials)]
                .scopes("read_scope"); //作用域
    }
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.authenticationManager(authenticationManager);
    }
}

然后打開SecurityAdapter,增加一個bean,如下,目的是讓上面的AuthorizationConfig里Autowired的authenticationManager可以實(shí)例化。

@Bean
    public AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

然后使用APIFox調(diào)用一下/oauth/token接口。

先選擇auth,輸入賬號密碼,這個賬號密碼就是AuthorizationConfig里配置的客戶端id和密碼。

image

這個數(shù)據(jù)在請求時,會進(jìn)行base64編碼,然后以http的header屬性Authorization的值的模式傳遞,如下。

image

然后輸入?yún)?shù),參數(shù)里scope和grant_type要和AuthorizationConfig里定義的scopes和authorizedGrantTypes一樣,如下。

image

請求后,得到結(jié)果,如上圖。

我們得到"access_token": "19d37af2-6e13-49c3-bf19-30a738b56886"。

有了access_token后,我們的前端其實(shí)就已經(jīng)可以進(jìn)行各種騷操作了。

資源服務(wù)

這個是Oauth為我們提供的一項(xiàng)很好用的功能。

我們創(chuàng)建一個項(xiàng)目做為資源服務(wù)。

添加依賴,版本與上面相同。

  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
            <version>2.4.0.RELEASE</version>
        </dependency>

然后編寫資源配置,代碼如下:

@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
    @Bean
    public RemoteTokenServices remoteTokenServices() {
        final RemoteTokenServices tokenServices = new RemoteTokenServices();
        tokenServices.setClientId("client-kiba");
        tokenServices.setClientSecret("kiba518-123456");
        tokenServices.setCheckTokenEndpointUrl("http://localhost:8080/oauth/check_token");//這個接口是oauth自帶的
        return tokenServices;
    }
    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        resources.stateless(true);
    }
    @Override
    public void configure(HttpSecurity http) throws Exception {
        //session創(chuàng)建策略
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED);
        //所有請求需要認(rèn)證
        http.authorizeRequests().anyRequest().authenticated();
    }
}

因?yàn)樘砑恿藄pring-boot-starter-security,所以,我們請求這個資源WebApp,就都需要輸入賬號密碼。

但因?yàn)?,我們配置了ResourceServerConfig,這里我們配置了遠(yuǎn)程token服務(wù),設(shè)置的信息是我們上面創(chuàng)建授權(quán)服務(wù)的信息。所以,在訪問這個WebApp時,我們提供token即可。

使用APIFOX測試,先添加auth的token,內(nèi)容是來自于上面,/oauth/token的返回值access_token的值。

image

然后請求user接口,我這user接口沒有參數(shù),請求結(jié)果如下:

image

總結(jié)

這個授權(quán)服務(wù)挺好用的,就是配置太繁瑣了,初學(xué)者不太好理解,而且功能太多,配置太鬧心。

這個資源服務(wù)還是很貼心的,他提我們實(shí)現(xiàn)了,tokencheck的部分,但要注意的是,他這tokencheck是基于http請求的。

雖然Oath很好用,但,我還是覺得,這個認(rèn)證部分自己寫比較好,我們可以根據(jù)項(xiàng)目的需求,設(shè)計(jì)輕量級的授權(quán)認(rèn)證。

比如,我們想減少http請求,把部分tokencheck在緩存內(nèi)進(jìn)行check,那使用oauth時,修改起來就會很頭疼。如果是自己寫的授權(quán)服務(wù)器,就不會有修改困難的問題。

注:此文章為原創(chuàng),任何形式的轉(zhuǎn)載都請聯(lián)系作者獲得授權(quán)并注明出處!

https://www.cnblogs.com/kiba/p/18252859

https://www.cnblogs.com/kiba/

到此這篇關(guān)于SpringBoot的Security和OAuth2的使用的文章就介紹到這了,更多相關(guān)SpringBoot的Security和OAuth2的使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論