一、基于注解方式用户授权:
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 在方法执行之前执行,而且这里可以调用方法的参数,然后对参数值进行过滤或处理或修改