介绍
如何在SpringBoot项目中使用SpringSecurity ?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
<强>依赖强>
& lt; dependencies> & lt; dependency> & lt; groupId> org.springframework.boot & lt; artifactId> spring-boot-starter-web & lt;/dependency> & lt; !——, Thymeleaf ——比; & lt; dependency> & lt; groupId> org.thymeleaf & lt; artifactId> thymeleaf-spring5 & lt;/dependency> & lt; dependency> & lt; groupId> org.thymeleaf.extras & lt; artifactId> thymeleaf-extras-java8time & lt;/dependency> & lt; !——, SpringSecurity ——比; & lt; dependency> & lt; groupId> org.springframework.boot & lt; artifactId> spring-boot-starter-security & lt;/dependency> & lt; !——, Thymeleaf 与,SpringSecurity 整合包,——比; & lt; dependency> ,& lt; groupId> org.thymeleaf.extras ,& lt; artifactId> thymeleaf-extras-springsecurity5 ,& lt; version> 3.0.4.RELEASE & lt;/dependency> & lt; dependency> & lt; groupId> org.springframework.boot & lt; artifactId> spring-boot-starter-test & lt; scope> test & lt; exclusions> & lt; exclusion> & lt; groupId> org.junit.vintage & lt; artifactId> junit-vintage-engine & lt;/exclusion> & lt;/exclusions> & lt;/dependency> & lt;/dependencies>
<强>控制器:强>
package com.blu.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @ controller public class  RouterController { @RequestMapping ({,“/?,“/index",}) public String 指数(),{ return “index"; } @RequestMapping (“/tologin") public String  toLogin (), { return “视图/login"; } @RequestMapping(“使/{id}“) public String 使(@PathVariable (“id"), int id), { return “视图/使“,+,id; } @RequestMapping(“二级/{id}“) public String 二级(@PathVariable (“id"), int id), { return “视图/二级/?+,id; } @RequestMapping (“/level3/{id}“) public String  level3 (@PathVariable (“id"), int id), { return “视图/level3/? +, id; } }
SecurityConfig:
package com.blu.config; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; @EnableWebSecurity public class  SecurityConfig extends  WebSecurityConfigurerAdapter {/* * ,*授权 ,*/@Override protected void 配置(HttpSecurity http), throws Exception {//所有人可以访问首页,功能页需要指定权限才可以访问 http.authorizeRequests () .antMatchers (“/? .permitAll () .antMatchers(“使/* *“).hasRole (“vip1") .antMatchers(“二级/* *“).hasRole (“vip2") .antMatchers (“/level3/* *“) .hasRole (“vip3");//没有权限将默认跳转至登录页,需要开启登录的页面//loginPage设置跳转至登录页的请求(默认为/登录)//usernameParameter和passwordParameter配置登录的用户名和密码参数名称,默认就是用户名和密码//loginProcessingUrl配置登录请求的url,需要和表单提交的url一致 http.formLogin () .loginPage (“/tologin") .usernameParameter (“username") .passwordParameter (“password") .loginProcessingUrl (“/login");//禁用CSRF保护 .disable http.csrf () ();//开启注销功能和注销成功后的跳转页面(默认为登录页面) http.logout () .logoutSuccessUrl (“/?;//开启记住我功能,饼干默认保存两周 http.rememberMe () .rememberMeParameter (“remember"); }/* * ,*认证 ,*/@Override protected void 配置(AuthenticationManagerBuilder 身份验证),throws Exception { auth.inMemoryAuthentication () .passwordEncoder (new BCryptPasswordEncoder ()) .withUser (“BLU") .password (new BCryptPasswordEncoder () .encode (“123456“)) .roles (“vip2",“vip3") , () .withUser (“root") .password (new BCryptPasswordEncoder () .encode (“111111“)) .roles (“vip1",“vip2",“vip3") , () .withUser (“guest") .password (new BCryptPasswordEncoder () .encode (“111222“)) .roles (“vip1"); } null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null如何在SpringBoot项目中使用SpringSecurity