最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Security學(xué)習(xí)筆記(一)

 更新時(shí)間:2020年09月03日 09:23:28   作者:mySoul  
這篇文章主要介紹了Spring Security的相關(guān)資料,幫助大家開(kāi)始學(xué)習(xí)Spring Security框架,感興趣的朋友可以了解下

介紹

這里學(xué)習(xí)SpringSecurity,對(duì)SpringSecurity進(jìn)行學(xué)習(xí)。

基本用法

添加依賴(lài)

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

啟動(dòng)項(xiàng)目

可以看到日志中,已經(jīng)有了密碼

訪(fǎng)問(wèn)接口,此時(shí)已經(jīng)有了登錄頁(yè)面

輸入用戶(hù)名和密碼

用戶(hù)名: user
密碼 984cccf2-ba82-468e-a404-7d32123d0f9c

此時(shí)已經(jīng)登錄成功

配置用戶(hù)名和密碼

在配置文件中,進(jìn)行配置

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

輸入用戶(hù)名和密碼,可以正常登錄

基于內(nèi)存的認(rèn)證

需要自定義類(lèi)繼承 WebSecurityConfigurerAdapter
實(shí)現(xiàn)自定義的配置
這里基于內(nèi)存的配置,如下

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

這里基于內(nèi)存的配置

HttpSecurity

這里對(duì)某些方法進(jìn)行攔截

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 {
  //基于內(nèi)存的用戶(hù)存儲(chǔ)
  @Override
  public void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication()
        .withUser("itguang").password("123456").roles("USER").and()
        .withUser("admin").password("{noop}" + "123456").roles("ADMIN");
  }





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


}

這里成功完成了post請(qǐng)求進(jìn)行登錄驗(yàn)證。

以上就是Spring Security學(xué)習(xí)筆記(一)的詳細(xì)內(nèi)容,更多關(guān)于Spring Security的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

获嘉县| 彭州市| 洪洞县| 年辖:市辖区| 崇仁县| 本溪市| 汉沽区| 广丰县| 祁阳县| 安多县| 西乡县| 惠来县| 商水县| 北碚区| 金昌市| 广灵县| 兴业县| 青神县| 翁源县| 临清市| 通辽市| 格尔木市| 武宁县| 任丘市| 永德县| 会宁县| 宜良县| 兴化市| 德昌县| 靖宇县| 泸定县| 灵石县| 高唐县| 盐源县| 绍兴市| 大荔县| 岳阳县| 青川县| 石阡县| 合江县| 康乐县|