springbootSecurity主要负责的是安全,里面自定义了拦截器和过滤器,使用springbootsecurity需要导入依赖:

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

导入这个依赖就可以调用springbootSecurity框架了,需要写一个配置文件:

package cn.com.zzn.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;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

//Aop :横切拦截器
@EnableWebSecurity //加入这个注解就被spring托管了
public class SecurityConfig extends WebSecurityConfigurerAdapter {

//    链式编程
    @Override
//    授权
    protected void configure(HttpSecurity http) throws Exception {
//        首页实现所有人可以访问,功能页只有对应有权限的人才能访问
//        请求授权的规则
        http.authorizeRequests().
//                首页所有人都能访问
                antMatchers("/").permitAll().
//                这个功能页只有vip1才能访问
                antMatchers("/level1/**").hasRole("vip1").
                antMatchers("/level2/**").hasRole("vip2").
                antMatchers("/level3/**").hasRole("vip3");

//        没有权限默认会到登陆页面  需要开启登录的页面
        http.formLogin();
//       防止网站注入 get post  springboot自动开启了csrf
        http.csrf().disable();//关闭跨站脚本请求攻击的功能,登陆失败可能存在的原因

//        注销 ,开启了注销功能,跳到首页
        http.logout().logoutSuccessUrl("/");
    }
//
    @Override
//    认证  , 在springboot 2.1.x 可以直接使用~
    //密码编码:PasswordEncoder
//    在spring Security 5.0+ 新增了很多方法 密码必须加密了才可以访问
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        这些数据正常来说应该在数据库读,但是目前没配置数据库,就先在内存里面读
//      设置哪些用户可以被认证
       auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                   .withUser("zzn").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3")
                  .and()//通过and符号拼接多个
                  .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3")
                  .and()
                  .withUser("xiaojiang").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");//这是从内存里面去认证,在内存中虚拟了一个数据
    }
}

在以上的配置文件中protected void configure(HttpSecurity http) throws Exception {}这个方法主要是进行授权,授权就是设置角色固定访问其页面,主要理解看以上代码。


这个方法主要是进行授权,给用户设置拥有的角色和密码,切记密码必须要用规定方式加密的,不然根本登不进去加密方式 new BCryptPasswordEncoder().encode("123456")

protected void configure(AuthenticationManagerBuilder auth) throws Exception {}

最后将这个结合前端来一起使用,怎么结合前端呢?首先要导入thymealeaf和Security的整合依赖:

<!--        springSecurity和thymeleaf整合包-->
        <!-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity4 -->
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
            <version>3.0.4.RELEASE</version>
        </dependency>

然后通过thymeleaf的sec来拿属性值和后端和前端结合,这样才可以就可以实现前后端交互的一些功能了。

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<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="@{/qinjiang/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">
                        用户名:<span sec:authentication="name"></span>
<!--                        角色:<span sec:authentication="principal.getAuthorities()"></span> -->
                        角色:<span sec:authentication="authorities"></span>
                    </a>
                </div>

                <!--已登录
                <a th:href="@{/usr/toUserCenter}">
                    <i class="address card icon"></i> admin
                </a>
                -->
                <div sec:authorize="isAuthenticated()">
                    <!--                如果登录:用户名,注销-->
                    <a class="item" th:href="@{/logout}">
                        <i class="window close icon"></i> 注销
                    </a>
                </div>

            </div>
        </div>
    </div>

    <div class="ui segment" style="text-align: center">
        <h3>Spring Security Study by 秦疆</h3>
    </div>

    <div>
        <br>
        <div class="ui three column stackable grid">
<!--            菜单根据用户的角色动态的实现-->
<!--            有vip1就显示-->
            <div class="column" sec:authorize="hasRole('vip1')">
                <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> Level-1-1</a></div>
                            <div><a th:href="@{/level1/2}"><i class="bullhorn icon"></i> Level-1-2</a></div>
                            <div><a th:href="@{/level1/3}"><i class="bullhorn icon"></i> Level-1-3</a></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="column">
                <div class="ui raised segment" sec:authorize="hasRole('vip2')">
                    <div class="ui">
                        <div class="content">
                            <h5 class="content">Level 2</h5>
                            <hr>
                            <div><a th:href="@{/level2/1}"><i class="bullhorn icon"></i> Level-2-1</a></div>
                            <div><a th:href="@{/level2/2}"><i class="bullhorn icon"></i> Level-2-2</a></div>
                            <div><a th:href="@{/level2/3}"><i class="bullhorn icon"></i> Level-2-3</a></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="column">
                <div class="ui raised segment" sec:authorize="hasRole('vip2')">
                    <div class="ui">
                        <div class="content">
                            <h5 class="content">Level 3</h5>
                            <hr>
                            <div><a th:href="@{/level3/1}"><i class="bullhorn icon"></i> Level-3-1</a></div>
                            <div><a th:href="@{/level3/2}"><i class="bullhorn icon"></i> Level-3-2</a></div>
                            <div><a th:href="@{/level3/3}"><i class="bullhorn icon"></i> Level-3-3</a></div>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>

</div>


<script th:src="@{/qinjiang/js/jquery-3.1.1.min.js}"></script>
<script th:src="@{/qinjiang/js/semantic.min.js}"></script>

</body>
</html>

over!