日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

spring boot整合spring security笔记

發(fā)布時間:2023/11/27 生活经验 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring boot整合spring security笔记 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

最近自己做了一個小項目,正在進行springboot和spring Security的整合,有一丟丟的感悟,在這里分享一下:

? ? 首先,spring boot整合spring security最好是使用Thymleaf,因為spring boot官方支持使用thymleaf,這樣的話,整合起來會方便很多,而且,thymleaf也支持靜態(tài)加載,比jsp方便許多。那么:

? ?第一步:需要先引入thymleafh和spring security的依賴(如果是web項目需要引入對應的jar包)?

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
? 第二步:在java配置類中去對spring security進行配置(也可以在xml中進行配置,
不過,spring boot比較推薦在代碼中進行配置,所以順勢而為了,就在代碼中
進行配置)
?
/**
* Spring Security 配置類.
*
*/
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true) // 啟用方法安全設置
public class SecurityConfig extends WebSecurityConfigurerAdapter {

private static final String KEY = "zhusz.com";

@Autowired
? private UserDetailsService userDetailsService;

@Autowired
? ?private PasswordEncoder passwordEncoder;

@Bean ?
? ?public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder(); // 使用 BCrypt 加密
? ?}

@Bean ?
? ?public AuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
authenticationProvider.setUserDetailsService(userDetailsService);
authenticationProvider.setPasswordEncoder(passwordEncoder); // 設置密碼加密方式
? ? ? ?return authenticationProvider;
}

/**
? ?* 自定義配置
? ?*/
? @Override
? protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/css/**", "/js/**", "/fonts/**", "/index").permitAll() // 都可以訪問
? ? ? ? ? ?.antMatchers("/h2-console/**").permitAll() // 都可以訪問
? ? ? ? ? ?.antMatchers("/admins/**").hasRole("ADMIN") // 需要相應的角色才能訪問
? ? ? ? ? ?.and()
.formLogin() //基于 Form 表單登錄驗證
? ? ? ? ? ?.loginPage("/login").failureUrl("/login-error") // 自定義登錄界面
? ? ? ? ? ?.and().rememberMe().key(KEY) // 啟用 remember me
? ? ? ? ? ?.and().exceptionHandling().accessDeniedPage("/403"); // 處理異常,拒絕訪問就重定向到 403 頁面
? ? ?http.csrf().ignoringAntMatchers("/h2-console/**"); // 禁用 H2 控制臺的 CSRF 防護
? ? ?http.headers().frameOptions().sameOrigin(); // 允許來自同一來源的H2 控制臺的請求
? }

/**
? ?* 認證信息管理
? ?* @param auth
? ?* @throws Exception
? ?*/
? @Autowired
? public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService);
auth.authenticationProvider(authenticationProvider());
}
}
此處的 ? userDetailsService 是spring security提供給我們的,不需要我們自己去編寫
還有
PasswordEncoder 
密碼加密也是spring security提供的。可以直接拿過來使用。自定義配置
是配置我們的過濾條件用的,此處可以使用前臺的remeber me來實現(xiàn)前臺的記住我
功能。
antMatchers("/h2-console/**").permitAll()表示都可以訪問/h2-console/路徑下的內(nèi)容
antMatchers("/admins/**").hasRole("ADMIN") 表示只有admin這樣的角色才可以
訪問/admins/路徑下的內(nèi)容。
具體的功能說明在代碼注釋中都有說明,可以參考一下。

轉(zhuǎn)載于:https://www.cnblogs.com/pigwood/p/10100606.html

總結(jié)

以上是生活随笔為你收集整理的spring boot整合spring security笔记的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。