• Spring security 知识笔记【自定义登录页面】


    一、引入依赖

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

    二、配置Spring Security的登录页面路径

      在WebSecurityConfig复写configure(HttpSecurityhttp)方法,复写登录页面的路径,如下示例代码:

    package Eleven.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
    import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
    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.security.crypto.password.PasswordEncoder;
    
    @Configuration
    @EnableWebSecurity
    @EnableGlobalMethodSecurity(prePostEnabled = true)
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
        @Bean
        public PasswordEncoder passwordEncoder(){
            return new BCryptPasswordEncoder();
        }
        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication().withUser("admin").password(passwordEncoder().encode("123456")).roles("admin");
            auth.inMemoryAuthentication().withUser("user").password(passwordEncoder().encode("123456")).roles("normal");
        }
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests() // 定义哪些URL需要被保护、哪些不需要被保护
                    .antMatchers("/login").permitAll()// 设置所有人都可以访问登录页面
                    .anyRequest().authenticated()  // 任何请求,登录后可以访问
                    .and()
                    .formLogin().loginPage("/login")
            ;
        }
    }

    三、自定义登录页面login.html

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="UTF-8">
        <title>My Login Page</title>
    </head>
    <body>
    <div th:if="${param.error}">
        用户名或密码错误!!!
    </div>
    <div th:if="${param.logout}">
        登出成功!!!
    </div>
    <form th:action="@{/login}" method="post">
        <div><label> 用户名: <input type="text" name="username"/> </label></div>
        <div><label> 密  码: <input type="password" name="password"/> </label></div>
        <div><input type="submit" value="登录"/></div>
    </form>
    </body>
    </html>

    四、自定义index.html页面

    <!DOCTYPE html>
    <html lang="en" >
    <head>
        <meta charset="UTF-8">
        <title>Spring Security</title>
    </head>
    <body>
    <h1>欢迎使用Spring Security!</h1>
    </body>
    </html>

    五、新建controller

    package Eleven.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    
    @Controller   //这里不能写成RestController,否则return后就是String类型了,而不是跳转到login.html
    public class HomeController {
        @GetMapping("/login")
        public String login(){
            return "/login";
        }
    
        @GetMapping({"","/","/index"})
        public String index() {
            return "/index";
        }
    
    
    }
  • 相关阅读:
    selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH.
    漫说996icu黑名单
    python datetime object 去除毫秒(microsecond)
    webpack4 系列教程(十四):Clean Plugin and Watch Mode
    webpack4 系列教程(十三):自动生成HTML文件
    webpack4 系列教程(十二):处理第三方JavaScript库
    webpack4 系列教程(十一):字体文件处理
    第一次遭遇云服务器完全崩溃
    music-api-next:一款支持网易、xiami和QQ音乐的JS爬虫库
    MathJax: 让前端支持数学公式
  • 原文地址:https://www.cnblogs.com/Eleven-Liu/p/11145970.html
Copyright © 2020-2023  润新知