spring-security關(guān)于hasRole的坑及解決
spring-security關(guān)于hasRole的坑
.access(“hasRole(‘ROLE_USER’)”) 如果用
.hasRole(“ROLE_USER”) 會(huì)報(bào)錯(cuò)。。。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘springSecurityFilterChain’ defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method ‘springSecurityFilterChain’ threw exception; nested exception is java.lang.IllegalArgumentException: role should not start with ‘ROLE_’ since it is automatically inserted. Got ‘ROLE_USER’
像下面代碼這樣是可以的,不要ROLE_USER,直接USER,不應(yīng)該用ROLE_開頭因?yàn)闀?huì)自動(dòng)加一個(gè)。
hasRole在進(jìn)行權(quán)限判斷時(shí)會(huì)被追加前綴ROLE_。
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/design", "/orders")
.hasRole("USER")
.antMatchers("/", "/**").access("permitAll")
.and()
.formLogin()
.loginPage("/login")
.and()
.logout()
.logoutSuccessUrl("/")
.and()
.csrf()
.ignoringAntMatchers("/h2-console/**")
.and()
.headers()
.frameOptions()
.sameOrigin()
;
}或者這樣也是可以的:
import org.springframework.security.config.annotation.web
.builders.HttpSecurity;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/design", "/orders")
.access("hasRole('ROLE_USER')")
.antMatchers("/", "/**").access("permitAll")
.and()
.formLogin()
.loginPage("/login")
.and()
.logout()
.logoutSuccessUrl("/")
.and()
.csrf()
.ignoringAntMatchers("/h2-console/**")
.and()
.headers()
.frameOptions()
.sameOrigin()
;
}還是應(yīng)該直接相信報(bào)錯(cuò)提示的,,,,,,走了太多彎路。
springboot整合spring-security注意事項(xiàng),不要踩坑
1.Spring Boot與Maven
Spring Boot提供了一個(gè)spring-boot-starter-security啟動(dòng)程序,它將Spring Security相關(guān)的依賴項(xiàng)聚合在一起。
利用啟動(dòng)器的最簡單和首選方法是使用IDE集成(Eclipse,IntelliJ,NetBeans)或通過https://start.spring.io使用Spring Initializr。
加入的依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>2.授權(quán)
我們的示例僅要求用戶進(jìn)行身份驗(yàn)證,并且已針對(duì)應(yīng)用程序中的每個(gè)URL進(jìn)行了身份驗(yàn)證。
我們可以通過向http.authorizeRequests()方法添加多個(gè)子項(xiàng)來指定網(wǎng)址的自定義要求。
例如:下面展示一些 內(nèi)聯(lián)代碼片。
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/resources/**", "/signup", "/about").permitAll()
.antMatchers("/admin/**").hasRole("ADMIN")
.antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')")
.anyRequest().authenticated()
.and()
// ...
.formLogin();
}- 1.http.authorizeRequests()方法有多個(gè)子項(xiàng),每個(gè)匹配器按其聲明的順序進(jìn)行考慮。
- 2.我們指定了任何用戶都可以訪問的多種URL模式。具體來說,如果URL以“/ resources /”開頭,等于“/ signup”或等于“/ about”,則任何用戶都可以訪問請(qǐng)求。
- 3.任何以“/ admin /”開頭的URL都將僅限于具有“ROLE_ADMIN”角色的用戶。您會(huì)注意到,由于我們正在調(diào)用hasRole方法,因此我們不需要指定“ROLE_”前綴。
- 4.任何以“/ db /”開頭的URL都要求用戶同時(shí)擁有“ROLE_ADMIN”和“ROLE_DBA”。您會(huì)注意到,由于我們使用的是hasRole表達(dá)式,因此我們不需要指定“ROLE_”前綴。
3.認(rèn)證
到目前為止,我們只看了最基本的身份驗(yàn)證配置。
我們來看一些稍微更高級(jí)的配置身份驗(yàn)證選項(xiàng)。
3.1內(nèi)存中認(rèn)證
我們已經(jīng)看到了為單個(gè)用戶配置內(nèi)存中身份驗(yàn)證的示例。
以下是配置多個(gè)用戶的示例:下面展示一些 內(nèi)聯(lián)代碼片。
@Bean
public UserDetailsService userDetailsService() throws Exception {
// ensure the passwords are encoded properly
UserBuilder users = User.withDefaultPasswordEncoder();
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
manager.createUser(users.username(“user”).password(“password”).roles(“USER”).build());
manager.createUser(users.username(“admin”).password(“password”).roles(“USER”,“ADMIN”).build());
return manager;
}3.2 JDBC身份驗(yàn)證
您可以找到支持基于JDBC的身份驗(yàn)證的更新。以下示例假定您已在應(yīng)用程序中定義了DataSource。
該JDBC-javaconfig樣品提供了使用基于JDBC認(rèn)證的一個(gè)完整的示例。
下面展示一些 `內(nèi)聯(lián)代碼片`。
@Autowired
private DataSource dataSource;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
// ensure the passwords are encoded properly
UserBuilder users = User.withDefaultPasswordEncoder();
auth
.jdbcAuthentication()
.dataSource(dataSource)
.withDefaultSchema()
.withUser(users.username("user").password("password").roles("USER"))
.withUser(users.username("admin").password("password").roles("USER","ADMIN"));
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java聊天室之實(shí)現(xiàn)接收和發(fā)送Socket
這篇文章主要為大家詳細(xì)介紹了Java簡易聊天室之實(shí)現(xiàn)接收和發(fā)送Socket功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以了解一下2022-10-10
Java五種方法批量處理List元素的實(shí)現(xiàn)示例
本文主要介紹了Java五種方法批量處理List元素的實(shí)現(xiàn)示例,包括傳統(tǒng)循環(huán)、函數(shù)式編程、Stream流、迭代器及第三方庫,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-05-05
Java采用循環(huán)鏈表結(jié)構(gòu)求解約瑟夫問題
這篇文章主要介紹了Java采用循環(huán)鏈表結(jié)構(gòu)求解約瑟夫問題的解決方法,是很多Java面試環(huán)節(jié)都會(huì)遇到的經(jīng)典考題,這里詳細(xì)給出了約瑟夫問題的原理及Java解決方法,是非常經(jīng)典的應(yīng)用實(shí)例,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12
Can''t use Subversion command line client:svn 報(bào)錯(cuò)處理
這篇文章主要介紹了Can't use Subversion command line client:svn 報(bào)錯(cuò)處理的相關(guān)資料,需要的朋友可以參考下2016-09-09
淺談java多態(tài)的實(shí)現(xiàn)主要體現(xiàn)在哪些方面
下面小編就為大家?guī)硪黄獪\談java多態(tài)的實(shí)現(xiàn)主要體現(xiàn)在哪些方面。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09
java遞歸設(shè)置層級(jí)菜單的實(shí)現(xiàn)
本文主要介紹了java遞歸設(shè)置層級(jí)菜單的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
java實(shí)現(xiàn)導(dǎo)出Excel的功能
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)導(dǎo)出Excel的功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05

