[Spring Cloud] - Spring Security實踐(一)- 基本概念及實踐

基本使用

Spring security須要的基本依賴:web

<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>

其餘部分不須要任何的增長。spring

Security的理論是兩個核心:認證(我是誰)和鑑權(我能作什麼)。
在code中,這兩個核心都須要經過繼承WebSecurityConfigurerAdapter來實現。app

➡️廢話很少說,上代碼ide

首先,確保yml中添加了上面提到的兩個依賴。添加後這就是最基本的spring security工程了。spring-boot

而後,咱們能夠先添加一些controller。好比IndexControllerui

@RestController
public class IndexController{
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(){
        return "index";
    }
}

此時啓動項目,會發現啓動log中夾雜着一句亂碼:加密

Using generated security password: 2465a939-a37d-4d3e-9ee1-05d2e51f18fb

這個「亂碼」就是spring security提供的缺省密碼。
此時訪問項目url,會自動跳轉到項目url/login頁面。
image.png
默認username爲user, password欄輸入剛剛那一句「亂碼」。
點擊signin,發現跳轉成功,會訪問到咱們最初訪問的頁面。url

自定義用戶名密碼

建立@configuration: spa

新建一個類,繼承 WebSecurityConfigurerAdapter, 添加註解@EnableWebSecurity (不用再添加@Configuration註解,由於已被EnableWebSecurity包含)以下所示。code

@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
}

覆寫父類方法:

覆寫configure(AuthenticationManagerBuilder auth)方法
⚠️ 父類中包含多個configure方法,注意選擇正確方法。代碼以下所示:

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
            .withUser("user").password(new BCryptPasswordEncoder().encode("123")).roles("USER");
}

此方法實現了三個功能:

  1. 定義了用戶名和密碼(user/123)
  2. 加密了密碼 - ⚠️ springsecurity強制密碼加密,此處必須這樣寫
  3. 定義此用戶的role爲USER - Spring security根據role來作鑑權操做,此處只是認證,暫時忽視便可。

此時,重啓項目,已經看不到最開始那一串亂碼了,使用user/123登錄,便可跳轉至正確頁面。

鑑權

覆寫方法
鑑權依靠的是另外一個方法: configure(HttpSecurity http),代碼以下:

@Override
    protected void configure(HttpSecurity http) throws Exception {
    }

示例代碼及註釋以下:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
         // 匹配 "/","/index" 路徑,不須要權限便可訪問
        .antMatchers("/user/user1").permitAll()
         // 匹配 "/admin" 及其如下全部路徑,都須要 "USER" 權限
        .antMatchers("/admin/**").hasRole("USER")
        .and()
        // 登陸地址爲 "/login",登陸成功默認跳轉到頁面 "/user"
        .formLogin().loginPage("/login").defaultSuccessUrl("/user/user1")
        .and()
        // 退出登陸的地址爲 "/logout",退出成功後跳轉到頁面 "/login"
        .logout().logoutUrl("/logout").logoutSuccessUrl("/login");
}
相關文章
相關標籤/搜索