1. 程式人生 > >spring securtty 學習(一)spring boot 中開啟spring securtty

spring securtty 學習(一)spring boot 中開啟spring securtty

cep () map read authorize tro quest row global

簡單來說,spring security提供Authentication認證和Authorization授權管理,其開發復雜度、學習難度都比shiro難,我們既然鐘情與spring再難也要學習,搞一搞。

pom.xml 加入security maven 依賴

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

application.yml 配置文件啟用
server:
    port: 8085 # 8085端口

security:
    basic:
       enabled: true 

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)  //  啟用方法級別的權限認證
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override 
    
protected void configure(HttpSecurity http) throws Exception { http.formLogin() // 表單登錄 // http.httpBasic() // HTTP Basic .and() .authorizeRequests() // 授權配置 .anyRequest() // 所有請求 .authenticated(); // 都需要認證 } }

簡單測試一下

@RestController
public class SecurityController {
    @GetMapping(
"hello") public String hello() { return "hello spring security"; } }

 

spring securtty 學習(一)spring boot 中開啟spring securtty