SpringBoot 2.x 集成SpringSecurity(二)之用户授权

一、基于注解方式用户授权:

 

JSR-250注解:

主要注解

  • @DenyAll 拒绝
  • @RolesAllowed 按角色允许,省略ROLE_前缀
  • @PermitAll 允许

 

1、Spring Security默认是关闭的,需要使用注解开启,代码如下:

package com.jefry.security;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;

@SpringBootApplication
@EnableGlobalMethodSecurity(jsr250Enabled = true)
@MapperScan("com.jefry.security.mapper")
public class SecurityApplication {

    public static void main(String[] args) {
        SpringApplication.run(SecurityApplication.class, args);
    }

}

 

2、在controller给不同用户增加不同的访问权限,代码如下:

 

package com.jefry.security.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.security.RolesAllowed;

@Slf4j
@RestController
public class hello {

    @GetMapping("/demo")
    public String demo() {
        return "Demo";
    }

    @GetMapping("/admin")
    @RolesAllowed("ADMIN")
    public String admin() {
        return "admin";
    }

    @GetMapping("/user")
    @RolesAllowed("USER")
    public String user() {
        return "user";
    }

    @GetMapping("/guest")
    @RolesAllowed("GUEST")
    public String guest() {
        return "guest";
    }
}

 

3、修改UserDetailService,给用户增加授权:

 

package com.jefry.security.service;

import com.jefry.security.entity.UserEntity;
import com.jefry.security.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;


@Component
@Slf4j
public class UserDetailService implements UserDetailsService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private UserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserEntity userEntity = userService.loadUsername(username);
        if (userEntity == null) {
            throw new UsernameNotFoundException("账号不存在");
        }
        String password = passwordEncoder.encode(userEntity.getPassword());
        log.info("登录账号:{},密码:{}", username, password);
        return new User(username, password, AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"));
    }
}

AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER") 就是给用户授于角色:USER,SpringSecurity的角色都需要统一加上ROLE_前缀。

访问http://127.0.0.1:8080/user 可以正常访问 ,/demo也可以正常访问,其它不可以;需要注意下账号授于的角色必须与RolesAllowed里的一致,不然不可以访问的,可以同时授多个角色如:RolesAllowed({"ADMIN","USER"})。

 

securedEnabled注解:

 

主要注解

  • @Secured
  • @Secured 在方法上指定安全性要求

 

可以使用@Secured在方法上指定安全性要求 角色/权限等 只有对应 角色/权限 的用户才可以调用这些方法。 如果有人试图调用一个方法,但是不拥有所需的 角色/权限,那会将会拒绝访问将引发异常。

 

代码示例:

@Secured("ROLE_ADMIN") @Secured({"ROLE_DBA", "ROLE_ADMIN"})

 

 

prePostEnabled注解:

 

主要注解

  • @PreAuthorize 进入方法之前验证授权
  • @PostAuthorize 检查授权方法之后才被执行
  • @PostFilter 在方法执行之后执行,而且这里可以调用方法的返回值,然后对返回值进行过滤或处理或修改并返回
  • @PreFilter 在方法执行之前执行,而且这里可以调用方法的参数,然后对参数值进行过滤或处理或修改

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值