当前位置: 首页 > news >正文

thymeleaf配套Security6

springboot3 security (一)简单模式

Spring Boot 2.x 及更早版本:
使用 spring.security.user.name 和 spring.security.user.password 直接在 application.properties 中配置默认用户是常见做法。
示例:

spring.security.user.name=admin
spring.security.user.password=123456

Spring Boot 2.7+ 和 Spring Security 5.7+:
官方开始弃用 spring.security.user.* 配置,推荐使用更灵活的 SecurityFilterChain 或新的配置方式。
现在写一个最简单的thymeleaf配套Security6的,登录页面是默认的,先用默认的,最简单模式

先写一个SecurityConfig.java

package cn.dails.Security;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;

/**
 * @author dzl
 */
@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
                .authorizeHttpRequests(auth -> auth
                        .requestMatchers("/").permitAll()
                        .anyRequest().authenticated()
                )
                .formLogin(form -> form
                        .loginPage("/login") // 默认登录页 URL
                        .permitAll()
                )
                .csrf(csrf -> csrf.disable()); // 临时禁用 CSRF(仅测试用)

        return http.build();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public UserDetailsService userDetailsService(PasswordEncoder passwordEncoder) {
        String userPassword = passwordEncoder.encode("123");
        String adminPassword = passwordEncoder.encode("456");


        UserDetails user = User.builder()
                .username("user")
                .password(userPassword)
                .roles("USER")
                .build();

        UserDetails admin = User.builder()
                .username("admin")
                .password(adminPassword)
                .roles("ADMIN")
                .build();

        return new InMemoryUserDetailsManager(user, admin);
    }
}

其次写一个LoginController ,配套一个index.html,让他拦截

package cn.dails.rest;


import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @author dzl
 */
@Slf4j
@Controller
public class LoginController {

    @GetMapping("/")
    public String home() {
        log.info("index");
        // 返回名为 "index" 的模板或 HTML 页面
        return "index";
    }
    

}


recourse下面建一个templates文件夹

里面写一个index.html

<html xml:th="https://www.thymeleaf.org" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <title>
        Hello Spring Security
    </title>
</head>
<body>
<h1>Hello Security</h1>
<a th:href="@{/logout}">Log Out 1 </a>
<a href="/logout">Log Out 2</a>
</body>
</html>

相关文章:

  • 如何将excel数据快速导入数据库
  • 【时频谱分析】快速谱峭度
  • 每日算法-250413
  • deskflow使用教程:一个可以让两台电脑鼠标键盘截图剪贴板共同使用的开源项目
  • 【开发工具】科研开发中的主流AI工具整理及如何使用GPT润色英文论文
  • 【苹果cms 1】本地影视资源站搭建
  • [文献阅读] chinese-roberta Pre-Training With Whole Word Masking for Chinese BERT
  • 真实企业级K8S故障案例:ETCD集群断电恢复与数据保障实践
  • QML ListView 与 C++ 模型交互
  • 微信小程序实战案例 - 餐馆点餐系统 阶段 0 - 环境就绪
  • 玩转Docker | 使用Docker部署MicroBin粘贴板
  • Java新手村第二站:泛型、集合与IO流初探
  • k8s的配置文件总结
  • Go学习路线指南
  • springboot框架集成websocket依赖实现物联网设备、前端网页实时通信!
  • MySQL——MVCC(多版本并发控制)
  • 免费在线文档工具,在线PDF添加空白页,免费在任意位置插入空白页,多样化的文件处理
  • 【AI论文】MM-IFEngine:迈向多模态指令遵循
  • Magnet Pro Macbook窗口分屏管理软件【提高效率工具】
  • 从零训练LLM-1.训练BPE
  • 优秀“博主”在上海杨浦购房最高补贴200万元,有何条件?
  • A股三大股指低收:银行股再度走强,两市成交11920亿元
  • 来论|建设性推进缅北和平进程——中国的智慧与担当
  • 成都公积金新政征求意见:购买保障性住房最高贷款额度上浮50%
  • 美联储主席:关税“远超预期”,美联储实现目标的进程或被推迟至明年
  • 中科院院士魏辅文已卸任江西农业大学校长