Spring Boot安全认证与Thymeleaf集成指南

引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity5</artifactId>
    <version>3.0.4.RELEASE</version>
</dependency>

配置类SecurityConfig

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    //授权
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/").permitAll()
                .antMatchers("/fail").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");
        http.formLogin().defaultSuccessUrl("/index")  //登录认证成功后默认转跳的路径
                .failureForwardUrl("/fail");
        //http.formLogin().usernameParameter("username").passwordParameter("password").loginPage("/toLogin").loginProcessingUrl("/login"); //.loginPage定制登录跳转页
        //http.logout().deleteCookies("remove").invalidateHttpSession(true);
        http.csrf().disable(); //关闭csrf功能
        http.logout().logoutSuccessUrl("/"); //开启注销功能,注销成功跳转到首页
        http.rememberMe().rememberMeParameter("remember"); //开启记住我功能
    }

    //认证
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("ls").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2", "vip3")
                .and()
                .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1", "vip2", "vip3")
                .and()
                .withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1", "vip2");
    }
}

index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="https://cdn.bootcss.com/semantic-ui/2.4.1/semantic.min.css">
</head>
<body>
<div>
    <div class="ui container">
        <div class="ui segment" id="index-header-nav" th:fragment="nav-menu">
            <div class="ui secondary menu">
                <a th:href="@{/index}" class="item">首页</a>
                <div class="right menu">

                    <div sec:authorize="!isAuthenticated()">
                        <!--自定义登录-->
<!--                        <a th:href="@{/toLogin}" class="item">-->
                            <!--security默认登陆-->
                            <a th:href="@{/login}" class="item">
                                <i class="address card icon">登录</i>
                            </a>
                    </div>
                    <div sec:authorize="isAuthenticated()">
                        <a href="" class="item">
                            用户名:<span sec:authentication="name"></span>
                            角色:<span sec:authentication="principal.authorities"></span>
                        </a>
                    </div>
                    <div sec:authorize="isAuthenticated()">
                        <a th:href="@{/logout}" class="item">
                            <i class="sign-out  icon">注销</i>
                        </a>
                    </div>
                </div>
            </div>
        </div>
        <div>
            <br>
            <div class="ui segment three column stackable grid">
                <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" sec:authorize="hasRole('vip2')">
                    <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>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" sec:authorize="hasRole('vip3')">
                    <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>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>
</div>

<script>
</script>
</body>
</html>

login.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="https://cdn.bootcss.com/semantic-ui/2.4.1/semantic.min.css">
</head>
<body>
<h1 style="text-align: center;margin-top: 50px">登录</h1>
<div class="ui column very relaxed stackable grid" style="width: 400px;margin: 60px auto">
    <div class="column">
        <div class="ui form">
            <form th:action="@{/login}" method="post">
                <div class="field">
                    <label >Username</label>
                    <div class="ui left icon input">
                        <input type="text" placeholder="请输入用户名" name="username">
                        <i class="user icon"></i>
                    </div>
                </div>

                <div class="field">
                    <label >Password</label>
                    <div class="ui left icon input">
                        <input type="password" placeholder="请输入密码" name="password">
                        <i class="lock icon"></i>
                    </div>
                </div>
                <input type="checkbox" name="remember">
                <input type="submit"  class="ui blue submit button">
            </form>
        </div>
    </div>
</div>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小蜜蜂127

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值