spring security 学习(一)spring boot 中开启spring security

简单来说,spring security提供Authentication认证和Authorization授权管理,其开发复杂度、学习难度都比shiro难,我们既然钟情与spring再难也要学习,搞一搞。

pom.xml 加入security maven 依赖

@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";
    }
}

 

 

posted @ 2019-03-10 12:19  andys  阅读(233)  评论(0编辑  收藏  举报