Springsecurity笔记14-18章JWT+Spring Security+redis+mysql 实现认证【动力节点】

15 SpringSecurity 集成thymeleaf

此项目是在springsecurity-12-database-authorization-method 的基础上进行
复制springsecurity-12-database-authorization-method 并重命名为springsecurity-13-thymeleaf

15.1 添加thymeleaf依赖

|

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>

15.2 修改application.yml

加入thymeleaf的配置

| spring:

thymeleaf:

cache: false # 不使用缓存check-template: true  # 检查thymeleaf模板是否存在 |

| — |

15.3 idea 添加thymeleaf模板

【File】—》【Settings…】

模板名称thymeleaf ,扩展名html,具体内容如下:

|

#[[$Title$]]#

#[[ E N D END END]]#

简要说明:

#[[ T i t l e Title Title]]# #[[ E N D END END]]# 这两处的作用是,当你新建一个模板页面时,在标签中输入标题内容后,只需要点击回车键,光标就会直接跳到内,省去了你挪动鼠标,或者挪动方向键的步骤,也可以给你节省一点点时间。<strong>新版****idea可能有点问题,实验一下Enable Live Template</strong>

15.4 新建LoginController

| @Controller

@RequestMapping(“/login”)

public class LoginController {

_/**

 * 跳转到登陆页面
 */

_@RequestMapping("/toLogin")

public String toLogin(){

    return "login";

}
}

15.5 创建thymeleaf文件login.html

在templates下面创建login.html,使用模板创建

|

<meta charset="UTF-8">

<title>用户登陆</title>

登录页面

<table>

    <tr>

        <td>用户名:</td>

        <td><input type="text" name="uname" value="thomas"></td>

    </tr>

    <tr>

        <td>密码:</td>

        <td><input type="password" name="pwd"></td>

    </tr>

    <tr>

        <td colspan="2">

            <button type="submit">登录</button>

        </td>

    </tr>

</table>

15.7 修改安全配置文件WebSecurityConfig

修改后如下:

| @EnableGlobalMethodSecurity(prePostEnabled = true)

//@Configuration

@Slf4j

public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Bean

public PasswordEncoder passwordEncoder(){

    return new BCryptPasswordEncoder();

}

@Override

protected void configure(HttpSecurity http) throws Exception {

    //设置登陆方式
    http.formLogin()//使用用户名和密码的登陆方式
            .usernameParameter("uname") //页面表单的用户名的name

            .passwordParameter("pwd")//页面表单的密码的name

            .loginPage("/login/toLogin") //自己定义登陆页面的地址
            .loginProcessingUrl("/login/doLogin")//配置登陆的url

            .successForwardUrl("/index/toIndex") //登陆成功跳转的页面
            .failureForwardUrl("/login/toLogin")//登陆失败跳转的页面
            .permitAll(); //放行和登陆有关的url,别忘了写这个
    //配置退出方式
    http.logout()

            .logoutUrl("/logout")

            .logoutSuccessUrl("/login/toLogin")

            .permitAll();/放行和退出有关的url,别忘了写这个


    //配置路径拦截 的url的匹配规则
    http.authorizeRequests()

            //任何路径要求必须认证之后才能访问
            .anyRequest().authenticated();

    // 禁用csrf跨站请求攻击  后面可以使用postman工具测试,注意要禁用csrf
    http.csrf().disable();

}
}

15.8 创建IndexController

| @Controller

@RequestMapping(“/index”)

public class IndexController {

_/**

 * 登录成功后进入主页
 */

_@RequestMapping("/toIndex")

public String toIndex(){

    return "main";

}
}

15.9 创建thymeleaf文件main.html

在templates下面创建main.html

|

<meta charset="UTF-8">

<title>系统首页</title>

系统首页

查询学生


添加学生


更新学生


删除学生


导出学生





退出


15.10 修改Studentcontroller

修改后如下:

| @Controller

@Slf4j

@RequestMapping(“/student”)

public class StudentController {

@GetMapping("/query")

@PreAuthorize("hasAuthority('student:query')")

public String queryInfo(){

    return "user/query";

}

@GetMapping("/add")

@PreAuthorize("hasAuthority('student:add')")

public String addInfo(){

    return "user/add";

}

@GetMapping("/update")

@PreAuthorize("hasAuthority('student:update')")

public String updateInfo(){

    return "user/update";

}

@GetMapping("/delete")

@PreAuthorize("hasAuthority('student:delete')")

public String deleteInfo(){

    return "user/delete";

}

@GetMapping("/export")

@PreAuthorize("hasAuthority('student:export')")

public String exportInfo(){

    return "/user/export";

}
}

15.11 在templates/user下面创建学生管理的各个页面

创建export.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>系统首页-学生管理</title>
</head>
<body>
<h1 align="center">系统首页-学生管理-导出</h1>
<a href="/index/toIndex">返回</a>
<br>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值