spring-boot-security

安全框架   

          在java開發領域,常見的安全管理框架有apache shiro和spring security。shiro是相對spring security是輕量級的,提供了認證、授權,會話管理,密碼管理、緩存管理等功能,spring security相對複雜點,功能比shiro更加強大,權限控制可以更精細,對OAuth2的支持也更友好,與spring框架無縫整合

spring security

簡單用法

官網demo
官網security架構

依賴

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <!-- 必須引入starter-web 否則不是web項目,不可能localhost:8080訪問,連錯誤頁面都沒有-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

登錄頁面

訪問 http://localhost:8080,出現了security的登錄頁面,不管是什麼URL都跳登錄,默認用戶名是user,默認密碼是每次啓動項目隨機生成

// 啓動時,生成隨機密碼
Using generated security password: 6a83bc58-fad7-4a16-94a3-9bd73d6a2f2f

如果不想使用隨機密碼,可以在yml中配

spring:
  security:
    user:  # 如果有了基於內存的認證,則此處配置不生效
      name: admin  
      password: admin
      roles: admin

postman訪問成功

基於內存的認證

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    PasswordEncoder passwordEncoder() {
        return NoOpPasswordEncoder.getInstance(); //如果沒有的話,會報java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { //這是基於內存的配置,則yml中配置的不生效
        auth.inMemoryAuthentication()
                .withUser("admin3").password("admin3").roles("ADMIN", "USER")
                .and()
                .withUser("user").password("123").roles("USER");
    }
}

 

如果你發現訪問任何頁面,都出現了上面的登錄頁面,八成是引入了security,刪除security即可解決,如果是因爲其他的依賴引入了security,就不太好刪除了,這時,下面的配置類可以解決,重寫 WebSecurityConfigurerAdapter即可

@Configuration
public class SecurityConfig {
    @Bean
    public WebSecurityConfigurerAdapter webSecurityConfigurerAdapter() {
        return new WebSecurityConfigurerAdapter() {
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                http.authorizeRequests()
                        .anyRequest().permitAll();
            }
        };
    }
}

 

基於角色的權限管理
  代表一系列行爲或責任的實體,限定能做什麼,不能做什麼,往往跟用戶賬號有關
  解決方案有
  1.Apache Shiro   使用簡單
  2.spring security 功能強大 兼容性強 spring家族
  <p>
  RBAC  基於角色的訪問控制 Role-Based-Access-Control,
  隱式訪問  如果是某個角色就顯示按鈕,否則不顯示,如果角色的名稱改了,代碼也要修改
  顯式訪問  如果擁有某種權限就顯示按鈕,否則不顯示
  spring security
  核心概念
  認證 authentication :認證是建立主體(principal)的過程,主體通常是指可以在您的應用程序中執行操作的用戶、設備或其他系統
  授權 authorization:或稱爲訪問控制 授權是指決定是否允許主體在應用程序中執行操作
  spring security 提供以下認證
  1.HTTP BASIC
  2.HTTP DIGEST
  3.HTTP x.509
  4.ldap 基於表單的認證
  5.openId
  6.單點登陸
  7.remember-me
  8.匿名身份登陸
  9.run-as
  10.JAAS
  11.Java EE容器認證
  模塊
  spring-security-core
  spring-security-remoting
  spring-security-web
  spring-security-config
  spring-security-ldap
  spring-security-acl 特定對象的實例進行安全配置
  spring-security-cas 單點登陸
  spring-security-openId 社會化的登陸
  spring-security-test 主要用與測試

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章