一、Spring Security中的认证 & 授权 & 角色继承
1.1、概述
关于Spring Security中的授权,请参考【系列一、认证 & 授权】,这里不再赘述。
1.2、资源类
/**
 * @Author : 一叶浮萍归大海
 * @Date: 2024/1/11 20:58
 * @Description: 测试资源
 */
@RestController
public class HelloController7003 {
    /**
     * 任何人都可以访问
     * @return
     */
    @GetMapping("/helloWorld")
    public R helloWorld() {
        return R.ok().data("Hello World");
    }
    /**
     * 登录后才能访问
     * @return
     */
    @GetMapping("/sayHi")
    public R sayHi() {
        return R.ok().data("嗨!");
    }
    /**
     * 需要具有dba角色的人才能访问
     * @return
     */
    @GetMapping("/dba/helloWorld")
    public R dba() {
        return R.ok().data("dba Hello World");
    }
    /**
     * 需要具有admin角色的人才能访问
     * @return
     */
    @GetMapping("/admin/helloWorld")
    public R admin() {
        return R.ok().data("admin Hello World");
    }
}1.3、配置类
/**
 * @Author : 一叶浮萍归大海
 * @Date: 2024/1/11 21:50
 * @Description: Spring Security配置类
 */
@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();
    }
    /**
     * 根据UserDetailsService定义基于内存的用户
     * @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()
                /**
                 * 登录成功 & 登录失败回调
                 */
                .formLogin()
                .loginPage("/login")
                .successHandler(successHandler)
                .failureHandler(failureHandler)
                .and()
                /**
                 * 注销登录回调
                 */
                .logout()
                .logoutUrl("/logout")
                .logoutSuccessHandler(logoutSuccessHandler)
                .permitAll()
                .and()
                .csrf()
                .disable()
                /**
                 * 未认证 & 权限不足回调
                 */
                .exceptionHandling()
                .authenticationEntryPoint(authenticationEntryPoint)
                .accessDeniedHandler(accessDeniedHandler);
    }
}1.4、测试
1.4.1、admin登录
(一) 登录
(二) 访问sayHi(登录就可以访问)
(三)访问/admin/helloWorld接口(需要拥有admin角色)
(四)访问/dba/helloWorld接口(需要拥有dba角色,admin自动继承dba角色)
1.4.2、dba登录
(一) 登录
(二) 访问sayHi(登录就可以访问)
(三)访问/admin/helloWorld接口(需要拥有admin角色,由于当前登录用户是dba,所以登录拒绝)
(四)访问/dba/helloWorld接口(需要拥有dba角色)


























