1. 程式人生 > 程式設計 >Spring Security學習筆記(一)

Spring Security學習筆記(一)

介紹

這裡學習SpringSecurity,對SpringSecurity進行學習。

基本用法

新增依賴

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

新增介面

package com.example.demo.web;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
public class Test {
  @RequestMapping("/test")
  public String test(){
    return "test";
  }
}

啟動專案

可以看到日誌中,已經有了密碼

Spring Security學習筆記(一)

訪問介面,此時已經有了登入頁面

Spring Security學習筆記(一)

輸入使用者名稱和密碼

使用者名稱: user
密碼 984cccf2-ba82-468e-a404-7d32123d0f9c

Spring Security學習筆記(一)

此時已經登入成功

配置使用者名稱和密碼

在配置檔案中,進行配置

spring:
security:
user:
name: ming
password: 123456
roles: admin

輸入使用者名稱和密碼,可以正常登入

基於記憶體的認證

需要自定義類繼承 WebSecurityConfigurerAdapter
實現自定義的配置
這裡基於記憶體的配置,如下

package com.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;


@Configuration
public class MyWebSecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
  PasswordEncoder passwordEncoder(){
    return NoOpPasswordEncoder.getInstance();
  }

  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication()
        .withUser("admin").password("123").roles("admin");
  }
}

這裡基於記憶體的配置

HttpSecurity

這裡對某些方法進行攔截

package com.ming.demo.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  //基於記憶體的使用者儲存
  @Override
  public void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication()
        .withUser("itguang").password("123456").roles("USER").and()
        .withUser("admin").password("{noop}" + "123456").roles("ADMIN");
  }





  //請求攔截
  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .anyRequest().permitAll()
        .and()
        .formLogin()
        .permitAll()
        .and()
        .logout()
        .permitAll();
  }


}

這裡成功完成了post請求進行登入驗證。

以上就是Spring Security學習筆記(一)的詳細內容,更多關於Spring Security的資料請關注我們其它相關文章!