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";
    }


}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM