Spring Security

Spring Security

引入maven依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

添加maven依赖后,加入@EnableWebSecurity就可以驱动Spring Sercurity.

配置WebSecurityConfigurerAdapter

/**
  *用来配置用户签名服务,主要是user-details机制,可以给用户赋予角色
  *@param auth 签名管理器构造器,用于构建用户具体权限控制
  */
protected void configure(AuthenticationManagerBuilder auth) throws Exception;
/**
  *用来配置拦截保护的请求,比如哪些请求需要放行,哪些需要验证
  *@param http 安全请求对象
  */
protected void configure(HttpSecurity http) throws Exception;
/**
  *用来配置Filter链
  *@param web 
  */
public void configure(WebSecurity web) throws Exception;

使用自定义用户认证服务

Spring Security提供了一个UserDetailsService接口,通过它可以获取用户信息,而这个接口只有一个loadUserByUsername方法需要实现,这个方法定义返回UserDetails接口对象,所以我们要开发一个UserAuthenticationService的接口,通过它可以操作Redis和数据库.

public class UserDetailsServiceImpl implements UserDetailsService {

    /**
     * 提供用户登录信息与角色权限信息
     */
    @Resource
    private UserAuthenticationService userAuthenticationService;
    /**
     *
     * @param username 用户名
     * @return a fully populated user record (never <code>null</code>)
     * @throws UsernameNotFoundException if the user could not be found or the user has no
     *                                   GrantedAuthority
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return userAuthenticationService.getConvoyUserDetail(username);
    }
}

UserAuthenticationService.class 提供用户身份与角色权限相关信息

public class UserAuthenticationService {
    @Resource
    private RedisService redisService;

    public CsuUserDetail getConvoyUserDetail(String userName) {
        // TODO: 2020/2/27 从数据库或缓存中获取用户信息
        return new CsuUserDetail();
    }
}

CsuUserDetail.class 实现security自带的UserDetail接口,继承系统封装好的用户-角色-权限相碰信息

public class CsuUserDetail extends CsuUserRolePrivilegeBO implements UserDetails {
    /**
     * 用户权限
     */
    private Collection<? extends GrantedAuthority> authorities;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }
    //省略
    }

限制请求(configure(HttpSecurity)方法)

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
    // 使用Spring表达式限定只有角色ROLE_USER或者ROLE_ADMIN
    .antMatchers("/user/**").access("hasRole('USER') or hasRole('ADMIN')")
    // 设置访问权限给角色ROLE_ADMIN,要求是完整登录(非记住我登录)
    .antMatchers("/admin/welcome1").
    access("hasAuthority('ROLE_ADMIN') && isFullyAuthenticated()")
    // 限定"/admin/welcome2"访问权限给角色ROLE_ADMIN,允许不完整登录
    .antMatchers("/admin/welcome2").access("hasAuthority('ROLE_ADMIN')")
    // 使用记住我的功能
    .and().rememberMe()
    // 使用Spring Security默认的登录页面
    .and().formLogin()
    // 启动HTTP基础验证
    .and().httpBasic();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值