Spring-security

2020-03-02  本文已影响0人  16325

概念

Spring-security是一个认证+鉴权的支持库,通过一系列filter来处理用户的认证,以及对后端接口权限的控制。

配置

image.png

截图里都是针对HttpSecurity请求的配置

 .authorizeRequests()
                    .anyRequest().authenticated()
                    .and()
.formLogin()
                    .loginPage("/auth/login").permitAll()
                    .usernameParameter("username")
                    .passwordParameter("password")
                    .loginProcessingUrl("/auth/doAuth")
                    .defaultSuccessUrl("/auth/index")
                    .failureForwardUrl("/auth/failed")
                    .and()

登录页面的处理

看到这里,都是登录的配置,但是具体的认证操作是在哪里处理的呢?

image.png

具体的一个AuthenticationProvider:


image.png

具体的UserDetailsService:


image.png

认证失败如何处理:


image.png

GrantedAuthorityDefaults

我们用注解来验证权限的时候

    @GetMapping("/delUser")
    @PreAuthorize("hasAnyRole('ADMIN','USER_ALL','USER_DELETE')")
    public String delete() {
        return "具有【用户删除】权限";
    }

验证的时候,通过查看 hasAnyRole源码,我们发现系统在做校验是默认会加一个角色前缀:private String defaultRolePrefix = "ROLE_";


image.png

如果不需要,可以在 WebSecurityConfig 中通过配置去掉这个默认前缀。

    @Bean
    GrantedAuthorityDefaults grantedAuthorityDefaults() {
        // Remove the ROLE_ prefix
        return new GrantedAuthorityDefaults("");
    }

PasswordEncoder

    @Bean
    public PasswordEncoder passwordEncoder() {
        // 密码加密方式
        return new BCryptPasswordEncoder();
    }

默认对密码字段都需要配置解密函数,同时要保证前台传来的是加密后的字符串。

注解方式鉴权

一、注解式方法级安全开启

需要在WebSecuirtyConfig添加配置:

@Configuration
@EnableWebSecurity //启用Spring Security.
////会拦截注解了@PreAuthrize注解的配置.
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
}

二、允许的注解

这里主要@PreAuthorize, @PostAuthorize, @Secured这三个注解可以使用。

@GetMapping("/helloUser")
@Secured({"ROLE_normal","ROLE_admin"})
public String helloUser() {
    return "hello,user";
}

说明:拥有normal或者admin角色的用户都可以方法helloUser()方法。另外需要注意的是这里匹配的字符串需要添加前缀“ROLE_“。
如果我们要求,只有同时拥有admin & noremal的用户才能方法helloUser()方法,这时候@Secured就无能为力了。

@GetMapping("/helloUser")
@PreAuthorize("hasAnyRole('normal','admin')")
public String helloUser() {
    return "hello,user";
}

说明:拥有normal或者admin角色的用户都可以方法helloUser()方法。
此时如果我们要求用户必须同时拥有normal和admin的话,那么可以这么编码: @PreAuthorize("hasRole('normal') AND hasRole('admin')")

@GetMapping("/helloUser")
@PostAuthorize(" returnObject!=null &&  returnObject.username == authentication.name")
public User helloUser() {
        Object pricipal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User user;
        if("anonymousUser".equals(pricipal)) {
            user = null;
        }else {
            user = (User) pricipal;
        }
        return user;
}

这三个最常用也就是@PreAuthorize这个注解了,在使用中主要是配合Spring EL表达式。

上一篇 下一篇

猜你喜欢

热点阅读