spring boot整合spring security

    技术2022-07-10  179

    spring boot整合spring security

    在开发中如果要对特定的页面进行拦截的话我们可以写拦截器但是这样的话会让我们的开发变得复杂起来,今天我就来介绍一下spring boot中是怎样做到页面的拦截的,首先spring boot中热门的有spring security 和shiro那么今天我就来说一下spring security

    一.页面的简单跳转

    1.编写MyController让其可以对页面进行跳转

    package com.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; /** * @Author big dog * Date on 2020/6/30 19:14 */ @Controller public class MyController { @RequestMapping("/index") public String index(){ return "index"; } @RequestMapping("/login") public String login(){ return "views/login"; } @RequestMapping("/level1/{id}") public String leve1(@PathVariable("id") int id){ return "views/level1/"+id; } @RequestMapping("/level2/{id}") public String leve2(@PathVariable("id") int id){ return "views/level2/"+id; } @RequestMapping("/level3/{id}") public String leve3(@PathVariable("id") int id){ return "views/level3/"+id; } }

    2.运行可以到首页面,点击页面可以进行跳转ok! 3.现在们导入spring security

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>

    然后重启项目然后可以发现现在spring security 已经帮我们做了拦截不登录就不能进入到首页中路

    二.配置自己的spring security

    经过上面的步骤我们就知道了我们要开始配置自己的spring security了,不然我们怎么进入首页呢是吧! SecurityConfig

    其中包含了登录注销各种功能,看不懂的可以联系我 做完这些后我们已经实现了登录拦截功能

    package com.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; /** * @Author big dog * Date on 2020/6/30 19:35 */ @EnableWebSecurity// 开启WebSecurity模式 public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { // 定制请求的授权规则 // 首页所有人可以访问 http.authorizeRequests().antMatchers("/index").permitAll() .antMatchers("/level1/**").hasRole("vip123") .antMatchers("/level2/**").hasRole("vip456") .antMatchers("/level3/**").hasRole("vip789"); // /登录功能login 请求来到登录页 http.formLogin().loginPage("/toLogin"); http.csrf().disable();//关闭防止攻击安全 // .logoutSuccessUrl("/index"); 注销成功来到首页 http.logout().logoutSuccessUrl("/index"); } //在内存中定义,也可以在jdbc中去拿.... //Spring security 5.0中新增了多种加密方式,也改变了密码的格式。 //要想我们的项目还能够正常登陆,需要修改一下configure中的代码。我们要将前端传过来的密码进行某种方式加密 //spring security 官方推荐的是使用bcrypt加密方式。 @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()) .withUser("goutou").password(new BCryptPasswordEncoder().encode("123456")).roles("vip123","vip456").and() .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip123","vip456","vip789"); } }

    三.深入改造

    1.做完上面的配置后我们就可以实现拦截了 但是我还要对上面的拦截做深入让每个用户看到的页面都是不相同的,比如你是root用户你可以进入全部的页面,但是普通用户只能看到他们自己的页面! 导入springsecurity依赖

    <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity5</artifactId> <version>3.0.4.RELEASE</version> </dependency>

    页面:

    <!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <title>首页</title> <!--semantic-ui--> <link href="https://cdn.bootcss.com/semantic-ui/2.4.1/semantic.min.css" rel="stylesheet"> <link th:href="@{/css/qinstyle.css}" rel="stylesheet"> </head> <body> <!--主容器--> <div class="ui container"> <div class="ui segment" id="index-header-nav" th:fragment="nav-menu"> <div class="ui secondary menu"> <a class="item" th:href="@{/index}">首页</a> <div class="right menu"> <!--如果未登录--> <div sec:authorize="!isAuthenticated()"> <a class="item" th:href="@{/toLogin}"> <i class="address card icon"></i> 登录 </a> </div> <!--如果已登录--> <div sec:authorize="isAuthenticated()"> <a class="item"> <i class="address card icon"></i> 用户名:<span sec:authentication="name"></span> </a> </div> <div sec:authorize="isAuthenticated()"> <a class="item" th:href="@{/logout}"> <i class="address card icon"></i> 注销 </a> </div> </div> </div> </div> <div class="ui segment" style="text-align: center" data-th-bgcolor="yellow"> <h3>欢 迎 来 到 德 莱 联 盟</h3> </div> <div> <br> <div class="ui three column stackable grid"> <div class="column" sec:authorize="hasRole('vip123')"> <div class="ui raised segment"> <div class="ui"> <div class="content"> <h5 class="content">Level 1</h5> <hr> <div><a th:href="@{/level1/1}"><i class="bullhorn icon"></i> VIP 1</a></div> <div><a th:href="@{/level1/2}"><i class="bullhorn icon"></i> VIP 2</a></div> <div><a th:href="@{/level1/3}"><i class="bullhorn icon"></i> VIP 3</a></div> </div> </div> </div> </div> <div class="column" sec:authorize="hasRole('vip456')"> <div class="ui raised segment"> <div class="ui"> <div class="content"> <h5 class="content">Level 2</h5> <hr> <div><a th:href="@{/level2/1}"><i class="bullhorn icon"></i> VIP 4</a></div> <div><a th:href="@{/level2/2}"><i class="bullhorn icon"></i> VIP 5</a></div> <div><a th:href="@{/level2/3}"><i class="bullhorn icon"></i> VIP 6</a></div> </div> </div> </div> </div> <div class="column" sec:authorize="hasRole('vip789')"> <div class="ui raised segment"> <div class="ui"> <div class="content"> <h5 class="content">Level 3</h5> <hr> <div><a th:href="@{/level3/1}"><i class="bullhorn icon"></i> VIP 7</a></div> <div><a th:href="@{/level3/2}"><i class="bullhorn icon"></i> VIP 8</a></div> <div><a th:href="@{/level3/3}"><i class="bullhorn icon"></i> VIP 9</a></div> </div> </div> </div> </div> </div> </div> </div> <script th:src="@{/js/jquery-3.1.1.min.js}"></script> <script th:src="@{/js/semantic.min.js}"></script> </body> </html>

    现在登录不同页面就可以只看到自己的那一部分了 root用户: goutou用户: 好了今天的分享就这些有需要源码的联系我! 相互学习

    Processed: 0.035, SQL: 12