千家信息网

spring security的简单例子分析

发表于:2024-09-29 作者:千家信息网编辑
千家信息网最后更新 2024年09月29日,这篇文章主要讲解了"spring security的简单例子分析",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"spring security的简单例子
千家信息网最后更新 2024年09月29日spring security的简单例子分析

这篇文章主要讲解了"spring security的简单例子分析",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"spring security的简单例子分析"吧!

1 pom.的主要文件 我引入的thymeleaf-extras-springsecurity5,springboot2.1.6 org.springframework.boot spring-boot-starter-thymeleaf

                org.thymeleaf.extras        thymeleaf-extras-springsecurity5            org.springframework.boot    spring-boot-starter-web            org.springframework.boot        spring-boot-starter-security                org.springframework.boot        spring-boot-starter-test        test    2 controller[@Controller](https://my.oschina.net/u/1774615)

public class KungfuController {

private final String PREFIX = "pages/";@RequestMapping("/")public String index() {        System.out.println("hello word");                return "welcome";}@RequestMapping("/userlogin")public String loginPage() {        return PREFIX+"login1";}@GetMapping("/level1/{path}")public String level1(@PathVariable("path")String path) {        return PREFIX+"level1/"+path;}@GetMapping("/level2/{path}")public String level2(@PathVariable("path")String path) {        return PREFIX+"level2/"+path;}@GetMapping("/level3/{path}")public String level3(@PathVariable("path")String path) {        return PREFIX+"level3/"+path;}

}

//配置下

@EnableWebSecurity public class mySecurity extends WebSecurityConfigurerAdapter {

//为啥引入这个bean ,因为在securety在5.0后使用系统的登录模板,默认把密码给加密啦,这个写的是不让密码加密

@Beanpublic static NoOpPasswordEncoder passwordEncoder() {    return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();}protected void configure(HttpSecurity http) throws Exception {   //定制请求的授权规则    http.authorizeRequests().antMatchers("/").permitAll()                    .antMatchers("/level1/**").hasRole("VIP1")                                    .antMatchers("/level2/**").hasRole("VIP2")                                    .antMatchers("/level3/**").hasRole("VIP3");                            //开启自动登录的功能         http.formLogin();  //开启自动配置的注销功能  http.logout().logoutSuccessUrl("/"); }public void configure(AuthenticationManagerBuilder auth) throws Exception {    auth.inMemoryAuthentication().withUser("mao").password("123").roles("VIP1","VIP2")                    .and().withUser("zhang").password("123").roles("VIP1","VIP3").and()                                    .withUser("li").password("123").roles("VIP2","VIP3");                        }

} //该模板都是尚学堂的

  xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity5">

Insert title here

欢迎光临武林秘籍管理系统

游客您好,如果想查看武林秘籍 请登录

,您好,您的角色有:

感谢各位的阅读,以上就是"spring security的简单例子分析"的内容了,经过本文的学习后,相信大家对spring security的简单例子分析这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!

0