SpringBoot中怎么整合SpringSecurity-創(chuàng)新互聯(lián)

SpringBoot中怎么整合SpringSecurity,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

站在用戶的角度思考問題,與客戶深入溝通,找到樂安網(wǎng)站設(shè)計與樂安網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都網(wǎng)站制作、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、申請域名、雅安服務(wù)器托管、企業(yè)郵箱。業(yè)務(wù)覆蓋樂安地區(qū)。

1.導(dǎo)包

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

2.測試

@RestController
public class HelloController
{
    @GetMapping("/hello")
    public String hello() {
        return "Hello";
    }
}

訪問http://localhost:8080/hello 會自動跳到login頁面
默認用戶名 user
默認密碼在控制臺顯示

二 在配置文件或代碼中配置security需要的用戶名和密碼

第一種: 在配置文件中配置

spring.security.user.name=user
spring.security.user.password=123
spring.security.user.roles=admin

第二種:在代碼中配置

新建一個SecurityConfig .java類

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
    //去掉spring5一定要密碼加密的限制
    @Bean
    PasswordEncoder passwordEncoder(){
        return NoOpPasswordEncoder.getInstance();
    }
    //第二種: 在代碼中配置用戶名和密碼
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception
    {
        auth.inMemoryAuthentication()
                .withUser("terry").password("123").roles("admin")
                .and()
                .withUser("tt").password("456").roles("user");
    }
}

三 配置HttpSecurity

1.接著上面的配置文件寫

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
    //去掉spring5一定要密碼加密的限制
    @Bean
    PasswordEncoder passwordEncoder(){
        return NoOpPasswordEncoder.getInstance();
    }
    //第二種: 在代碼中配置用戶名和密碼
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception
    {
        auth.inMemoryAuthentication()
                .withUser("terry").password("123").roles("admin")
                .and()
                .withUser("tt").password("456").roles("user");
    }

    //HttpSecurity配置
    @Override
    protected void configure(HttpSecurity http) throws Exception
    {
        http.authorizeRequests()
                .antMatchers("/admin/**").hasRole("admin")
                .antMatchers("/user/**").hasAnyRole("admin","user")
                //.antMatchers("/user/**").access("hasAnyRole('user','admin')")
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginProcessingUrl("/doLogin")
                .permitAll()
                .and()
                .csrf().disable();//要使用postman,防止被認為是csrf攻擊
    }
}

2.測試

@RequestMapping("/admin/hello")
public String admin(){
    return "Hello admin";
}

@RequestMapping("/user/hello")
public String user(){
    return "hello user";
}

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。

名稱欄目:SpringBoot中怎么整合SpringSecurity-創(chuàng)新互聯(lián)
網(wǎng)頁地址:http://www.muchs.cn/article34/dppdse.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、網(wǎng)站導(dǎo)航、電子商務(wù)標簽優(yōu)化、網(wǎng)頁設(shè)計公司虛擬主機

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護公司