一、Spring Security中的認(rèn)證 & 授權(quán) & 角色繼承
1.1、概述
? ? ? ? 關(guān)于Spring Security中的授權(quán),請(qǐng)參考【系列一、認(rèn)證 & 授權(quán)】,這里不再贅述。
1.2、資源類(lèi)
/**
* @Author : 一葉浮萍?xì)w大海
* @Date: 2024/1/11 20:58
* @Description: 測(cè)試資源
*/
@RestController
public class HelloController7003 {
/**
* 任何人都可以訪問(wèn)
* @return
*/
@GetMapping("/helloWorld")
public R helloWorld() {
return R.ok().data("Hello World");
}
/**
* 登錄后才能訪問(wèn)
* @return
*/
@GetMapping("/sayHi")
public R sayHi() {
return R.ok().data("嗨!");
}
/**
* 需要具有dba角色的人才能訪問(wèn)
* @return
*/
@GetMapping("/dba/helloWorld")
public R dba() {
return R.ok().data("dba Hello World");
}
/**
* 需要具有admin角色的人才能訪問(wèn)
* @return
*/
@GetMapping("/admin/helloWorld")
public R admin() {
return R.ok().data("admin Hello World");
}
}
1.3、配置類(lèi)
/**
* @Author : 一葉浮萍?xì)w大海
* @Date: 2024/1/11 21:50
* @Description: Spring Security配置類(lèi)
*/
@Configuration
public class MyWebSecurityConfigurerAdapter7003 extends WebSecurityConfigurerAdapter {
@Resource
private MyAuthenticationSuccessHandler7003 successHandler;
@Resource
private MyAuthenticationFailureHandler7003 failureHandler;
@Resource
private MyLogoutSuccessHandler7003 logoutSuccessHandler;
@Resource
private MyAuthenticationEntryPoint7003 authenticationEntryPoint;
@Resource
private MyAccessDeniedHandler7003 accessDeniedHandler;
/**
* 密碼加密器
* @return
*/
@Bean
PasswordEncoder passwordEncoder() {
return NoOpPasswordEncoder.getInstance();
}
/**
* 根據(jù)UserDetailsService定義基于內(nèi)存的用戶
* @return
*/
@Bean
protected UserDetailsService userDetailsService() {
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
manager.createUser(User.withUsername("dba").password("123456").roles("dba").build());
manager.createUser(User.withUsername("admin").password("123456").roles("admin").build());
return manager;
}
/**
* 角色繼承
* @return
*/
@Bean
protected RoleHierarchy roleHierarchy() {
RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl();
roleHierarchy.setHierarchy("ROLE_admin > ROLE_dba");
return roleHierarchy;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/dba/**").hasRole("dba")
.antMatchers("/admin/**").hasRole("admin")
.antMatchers("/helloWorld")
.permitAll()
.anyRequest()
.authenticated()
.and()
/**
* 登錄成功 & 登錄失敗回調(diào)
*/
.formLogin()
.loginPage("/login")
.successHandler(successHandler)
.failureHandler(failureHandler)
.and()
/**
* 注銷(xiāo)登錄回調(diào)
*/
.logout()
.logoutUrl("/logout")
.logoutSuccessHandler(logoutSuccessHandler)
.permitAll()
.and()
.csrf()
.disable()
/**
* 未認(rèn)證 & 權(quán)限不足回調(diào)
*/
.exceptionHandling()
.authenticationEntryPoint(authenticationEntryPoint)
.accessDeniedHandler(accessDeniedHandler);
}
}
1.4、測(cè)試
1.4.1、admin登錄
(一) 登錄
(二) 訪問(wèn)sayHi(登錄就可以訪問(wèn))
(三)訪問(wèn)/admin/helloWorld接口(需要擁有admin角色)?
(四)訪問(wèn)/dba/helloWorld接口(需要擁有dba角色,admin自動(dòng)繼承dba角色)?
1.4.2、dba登錄
(一) 登錄
(二) 訪問(wèn)sayHi(登錄就可以訪問(wèn))
(三)訪問(wèn)/admin/helloWorld接口(需要擁有admin角色,由于當(dāng)前登錄用戶是dba,所以登錄拒絕)?
(四)訪問(wèn)/dba/helloWorld接口(需要擁有dba角色)?文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-788931.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-788931.html
到了這里,關(guān)于系列六、Spring Security中的認(rèn)證 & 授權(quán) & 角色繼承的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!