spring boot整合spring security笔记
生活随笔
收集整理的這篇文章主要介紹了
spring boot整合spring security笔记
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
最近自己做了一個小項目,正在進(jìn)行springboot和spring Security的整合,有一丟丟的感悟,在這里分享一下:
? ? 首先,spring boot整合spring security最好是使用Thymleaf,因為spring boot官方支持使用thymleaf,這樣的話,整合起來會方便很多,而且,thymleaf也支持靜態(tài)加載,比jsp方便許多。那么:
? ?第一步:需要先引入thymleafh和spring security的依賴(如果是web項目需要引入對應(yīng)的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進(jìn)行配置(也可以在xml中進(jìn)行配置,
不過,spring boot比較推薦在代碼中進(jìn)行配置,所以順勢而為了,就在代碼中
進(jìn)行配置)
?
/**
* Spring Security 配置類.
*
*/
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true) // 啟用方法安全設(shè)置
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); // 設(shè)置密碼加密方式
? ? ? ?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") // 需要相應(yīng)的角色才能訪問
? ? ? ? ? ?.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 防護(hù)
? ? ?http.headers().frameOptions().sameOrigin(); // 允許來自同一來源的H2 控制臺的請求
? }
/**
? ?* 認(rèn)證信息管理
? ?* @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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 清明祭祀为何说“清明拜山”?
- 下一篇: 对python3中pathlib库的Pa