Spring Security - 04 使用表单登录认证方式

Spring Security - 04 使用表单登录认证方式,第1张

Spring Security - 04 使用表单登录认证方式

文章目录

环境项目结构测试参考

环境

*** 作系统:

Windows 10 x64

集成开发环境:

Spring Tool Suite 4 
Version: 4.12.1.RELEASE
Build Id: 202110260750

浏览器(客户端):

Google Chrome
版本 97.0.4692.71(正式版本) (64 位)
项目结构

参考:Spring Security - 03 使用 Basic HTTP 认证方式

修改 WebSecurityConfigurer 配置类,使用表单登录认证方式(第 13 行):

package com.mk.security.config.annotation.web.configuration;

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;

//@Configuration
@EnableWebSecurity
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin();
        http.authorizeRequests((requests) -> requests.anyRequest().authenticated());
//        http.authorizeRequests().anyRequest().authenticated();
    }
}
测试

启动项目,打开浏览器,如果你已经登录,那么请先清除过去一小时的浏览数据,防止遗留的认证信息干扰本次测试。

访问 http://localhost:8080/principal,由于我们没有通过身份认证,Spring Security 会将我们重定向至 http://localhost:8080/login,现在看到就是表单登录页面:

参考

Spring Security > Servlet Applications > Authentication > Username/Password > Reading Username/Password > Form / Form Login

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/zaji/5721834.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存