色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

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

瀏覽:3日期:2022-08-25 15:45:13

介紹

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

基本用法

添加依賴

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

啟動項目

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

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

訪問接口,此時已經(jīng)有了登錄頁面

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

輸入用戶名和密碼

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

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

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

配置用戶名和密碼

在配置文件中,進行配置

spring:security:user:name: mingpassword: 123456roles: admin

輸入用戶名和密碼,可以正常登錄

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

需要自定義類繼承 WebSecurityConfigurerAdapter實現(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;@Configurationpublic 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

這里對某些方法進行攔截

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@EnableWebSecuritypublic class SecurityConfig extends WebSecurityConfigurerAdapter { //基于內(nèi)存的用戶存儲 @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學(xué)習(xí)筆記(一)的詳細(xì)內(nèi)容,更多關(guān)于Spring Security的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 99热久久精品国产 | 亚洲欧美日本综合一区二区三区 | 亚洲免费视频在线观看 | 日韩一级片 | 亚洲精品久久片久久 | 日韩成人在线观看视频 | 日本成人免费在线视频 | 国产成人一区二区三区在线播放 | 毛片美国基地 | 亚洲偷自拍另类图片二区 | 美女黄频免费观看 | 女人张开腿男人猛桶视频 | 国产片18在线观看 | 亚洲成人在线免费视频 | 中文字幕一区二区在线播放 | 99视频精品全国免费 | a成人在线 | 91av视频在线 | 91网站在线免费观看 | 夜间福利网站 | 精品欧美成人高清视频在线观看 | 狠狠色丁香九九婷婷综合五月 | 久久厕所精品国产精品亚洲 | 精品国产网站 | 国产91区| 欧美一级片播放 | 99视频在线观看高清 | 狠狠se| 66精品 | 国产精品高清视亚洲精品 | 男女性高爱潮免费的国产 | 精品国产不卡一区二区三区 | 久久综合一区二区三区 | 国产欧美日韩成人 | 91成人国产网站在线观看 | 国产一区精品在线观看 | 亚洲精品一区二区三区五区 | 99re66热这里只有精品免费观看 | 欧美高清一区二区三区欧美 | 久久视频这里只有精品 | 欧美成人免费全部观看天天性色 |