Simple example Analysis of spring security
This article mainly explains "simple example Analysis of spring security". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "simple example Analysis of spring security".
1 pom. The main file I introduced into 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;}
}
/ / configure
@ EnableWebSecurity public class mySecurity extends WebSecurityConfigurerAdapter {
/ / Why is this bean introduced, because after securety uses the login template of the system after 5.0, the password is encrypted by default, and this one is written not to encrypt the password.
@ Beanpublic static NoOpPasswordEncoder passwordEncoder () {return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance () } protected void configure (HttpSecurity http) throws Exception {/ / Custom request authorization rules http.authorizeRequests (). AntMatchers ("/"). PermitAll () .antMatrices ("/ level1/**"). HasRole ("VIP1") .antMatrices ("/ level2/**"). HasRole ("VIP2") .antMatch ("/ level3/**") .hasRole ("VIP3") / / enable automatic login function http.formLogin (); / / enable automatic configuration logout function 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 ("VIP2", "VIP3") }
} / / the templates are all from Shangxue School.
Insert title here
Welcome to Wulin Secret Book Management system
Hello, tourists. If you want to see the secret books of Wulin, please log in.
Hello, your roles are: thank you for reading, the above is the content of "simple example Analysis of spring security". After the study of this article, I believe you have a deeper understanding of the simple example analysis of spring security, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!