Spring Boot集成Spring Security

  • 引入依赖
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests()// 开启登录配置.antMatchers("/static/**").permitAll()// 静态资源可访问.antMatchers("/web/**").permitAll()// 静态资源可访问.antMatchers("/").permitAll()// 项目启动.antMatchers("/index.html").permitAll()// 可直接访问,无限制.anyRequest().authenticated()// 其他请求,登录后可以访问.and().formLogin().loginPage("/login")// 自定义登录页面,未登录时,访问一个受限制的接口,会自动跳转到该页面.permitAll()// 和表单登录相关的接口统统都直接通过.and().csrf().disable() // post请求需要csrf验证, 这里使用Thymeleaf模板引擎,表单默认发送csrf,可不用关闭;}@Autowiredprivate AuthenticationProvider provider;  //注入我们自己的AuthenticationProvider@Overrideprotected void configure(AuthenticationManagerBuilder auth) {auth.authenticationProvider(provider);}}

  •  添加一个控制器,对应/login 返回一个登录页面;并配置页面跳转的方法
@Controller
public class WorkSpaceAction {/*** 系统默认登录接口*/@RequestMapping("/login")public String login() {return "login";}/*** 页面跳转*/@RequestMapping("{url}.html")public String page(@PathVariable("url") String url, RedirectAttributes attributes, HttpServletRequest request, Model model) {pNames(attributes, request, model);return url;}/*** 页面跳转(二级目录)*/@RequestMapping("{module}/{url}.html")public String page(@PathVariable("module") String module, @PathVariable("url") String url, RedirectAttributes attributes, HttpServletRequest request, Model model) {System.out.println("二级页面>> " + module + " " + url);pNames(attributes, request, model);return module + "/" + url;}/*** 页面跳转(三级目录)*/@RequestMapping("{module1}/{module2}/{url}.html")public String page(@PathVariable("module1") String module1, @PathVariable("module2") String module2, @PathVariable("url") String url, RedirectAttributes attributes, HttpSession session, HttpServletRequest request, Model model) {System.out.println("三级页面>> " + module1 + " " + url);pNames(attributes, request, model);return module1 + "/" + module2 + "/" + url;}private void pNames(RedirectAttributes attributes, HttpServletRequest request, Model model) {Enumeration pNames = request.getParameterNames();while (pNames.hasMoreElements()) {String name = (String) pNames.nextElement();String value = request.getParameter(name);System.out.println(name + "=" + value);attributes.addAttribute(name, value);attributes.addFlashAttribute(name, value);model.addAttribute(name, value);}}
}

  • html页面是使用 thymeleaf 模板引擎的,这里需要在配置文件中进行配置 
#thymelea模板配置
spring.thymeleaf.check-template=true
#热部署文件,页面不产生缓存,及时更新
spring.thymeleaf.cache=false
#文档MIME类型是text/html,也就是html格式的文档
spring.thymeleaf.servlet.content-type=text/html
#采用utf-8编码,避免页面乱码
spring.thymeleaf.encoding=UTF-8
#页面是html类型
spring.thymeleaf.mode=HTML
#html页面在resources文件夹下的templates文件夹中
spring.thymeleaf.prefix=classpath:/templates/
#页面后缀是html格式
spring.thymeleaf.suffix=.html

  •  登录页面:需要引入cmdEncrypt.js、jsencrypt.min.js用于密码加密;jsencrypt前端js分段加密解密RSA;


 cmdEncrypt.js、jsencrypt.min.js下载链接:https://download.csdn.net/download/weking1024/12022181


  • 自定义用户名和密码
@Service
public class MyUserDetailsService implements UserDetailsService {@Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {//这里可以通过数据库来查找到实际的用户信息,这里我们先模拟下,后续我们用数据库来实现if (username.equals("admin")) {//假设返回的用户信息如下;UserInfo userInfo = new UserInfo();userInfo.setUserName("admin");userInfo.setPassword("e9cdea73d73d2f28c1e5644339eba098");return userInfo;}return null;}
}

 


  •  加载静态资源
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {private static final String STATIC_LOCATION = "/static/";public void addResourceHandlers(ResourceHandlerRegistry registry) {// 这里是配置静态资源文件的路径registry.addResourceHandler(STATIC_LOCATION + "**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + STATIC_LOCATION);}//直接页面跳转,不经过Controller,这样在没有任何处理业务的时候,快捷的页面转向定义会节省好多代码@Overridepublic void addViewControllers(ViewControllerRegistry registry) {registry.addViewController("/").setViewName("index.html"); // 项目启动默认跳转页面}}

 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部