0、pom
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://.jack.demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.security.access.prepost.PostAuthorize;import org.springframework.security.access.prepost.PostFilter;import org.springframework.security.access.prepost.PreAuthorize;import org.springframework.security.access.prepost.PreFilter;import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;import org.springframework.security.core.userdetails.User;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;@EnableGlobalMethodSecurity(prePostEnabled = true)@RestController@SpringBootApplicationpublic class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @RequestMapping("/") public String index(){ return "hello Spring Security!"; } @RequestMapping("/hello") public String hello(){ return "hello !"; } @PreAuthorize("hasRole('ROLE_ADMIN')") @RequestMapping("/roleAdmin") public String role() { return "admin auth"; } @PreAuthorize("#id<10 and principal.username.equals(#username) and #user.username.equals('abc')") @PostAuthorize("returnObject%2==0") @RequestMapping("/test") public Integer test(Integer id, String username, User user) { // ... return id; } @PreFilter("filterObject%2==0") @PostFilter("filterObject%4==0") @RequestMapping("/test2") public List<Integer> test2(List<Integer> idList) { // ... return idList; }}测试hello接口(http://localhost:8080/hello)
未登录跳转登录页
登录SpringSecurityConfig配置的admin账号与密码123456
成功调用hello
测试roleAdmin(登录admin 123456成功,登录jack fang访问则失败)
登出 logout
到此这篇关于详解Springboot2.3集成Spring security 框架(原生集成)的文章就介绍到这了,更多相关Springboot2.3集成Spring security 内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!