详解Springboot2.3集成Spring security 框架(原生集成)

时间:2021-05-19

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 内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章