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

您的位置:首頁技術文章
文章詳情頁

Spring Security學習筆記(一)

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

介紹

這里學習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: mingpassword: 123456roles: 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;@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'); }}

這里基于內存的配置

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 { //基于內存的用戶存儲 @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的資料請關注好吧啦網其它相關文章!

標簽: Spring
相關文章:
主站蜘蛛池模板: 日本高清免费视频色www | 男人的天堂毛片 | 特黄a三级三级三级 | 国产精品精品国产一区二区 | 鲁老汉精品视频在线观看 | 亚洲国产成+人+综合 | 中文字幕综合在线 | 欧美日韩高清不卡一区二区三区 | 欧美野外性k8播放性迷宫 | 国产精品久久久久久搜索 | 在线观看亚洲国产 | 久久99亚洲精品久久99 | 中文字幕最新中文字幕中文字幕 | 久久久久久久亚洲精品一区 | 清纯偷拍精品视频在线观看 | 一个人看的免费高清视频日本 | 国产一区二区三区视频在线观看 | 免费在线一级毛片 | 美女18网站 | 国产乱码一区二区三区四川人 | 久久综合99re88久久爱 | 成人国产精品免费软件 | 日韩专区亚洲精品欧美专区 | 久久精品午夜 | 欧美在线高清视频播放免费 | www久久| 日韩专区亚洲精品欧美专区 | 亚洲第一成年网站大全亚洲 | 最新国产精品视频免费看 | 欧美一级毛片在线观看 | 丁香伊人五月综合激激激 | 午夜欧美| 美女视频全部免费 | 日韩一区二区三区视频 | 韩国免又爽又刺激激情视频 | 黄色福利站 | 99精品国产高清一区二区三区香蕉 | 一级特黄特色的免费大片视频 | 亚洲性综合 | 91免费永久国产在线观看 | 亚洲精品欧美精品一区二区 |