广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringSecurity自定义登录界面
  • 206
分享到

SpringSecurity自定义登录界面

2024-04-02 19:04:59 206人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

为什么需要自定义登录界面? 答:因为SpringBoot整合springSecurity时,只需要一个依赖,无需其他配置,就可以实现认证功能。但是它的认证登录界面是固定那样的,如下图

为什么需要自定义登录界面?

答:因为SpringBoot整合springSecurity时,只需要一个依赖,无需其他配置,就可以实现认证功能。但是它的认证登录界面是固定那样的,如下图所示,但是我们希望自己搞个好看的登录界面,所以需要自定义登录界面。

第一步:创建springboot项目

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="Http://Maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>spring-security-03</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-security-03</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--thymeleaf-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-WEB</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

第二步:添加配置application.properties

#修改springSecurity默认用户名和密码
spring.security.user.name=root
spring.security.user.passWord=root

#设置 thymeleaf 缓存为false,表示立即生效
spring.thymeleaf.cache=false

第三步:Controller

package com.example.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello(){
        System.out.println("hello spring security");
        return "hello spring security";
    }
}
package com.example.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexController {

    @RequestMapping("/index")
    public String hello(){
        System.out.println("hello index");
        return "hello index";
    }
}
package com.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class LoginController {

    @RequestMapping("/loginhtml")
    public String loginHtml(){
        return "login";
    }
}

第四步:login.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org/" lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户登录</title>
</head>
<body>
    <h1>用户登录</h1>
    <fORM th:action="@{/doLogin}" method="post">
        用户名:<input type="text" name="username"> <br>
        密码:<input type="text" name="password"><br>
        <input type="submit" value="登录">
    </form>
</body>
</html>

第五步:配置自定义登录界面

package com.example.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {


    @Override
    public void configure(HttpSecurity http) throws Exception {

        //【注意事项】放行资源要放在前面,认证的放在后面
        http.authorizeRequests()
                .mvcMatchers("/index").permitAll() //代表放行index的所有请求
                .mvcMatchers("/loginHtml").permitAll() //放行loginHtml请求
                .anyRequest().authenticated()//代表其他请求需要认证
                .and()
                .formLogin()//表示其他需要认证的请求通过表单认证
                //loginPage 一旦你自定义了这个登录页面,那你必须要明确告诉SpringSecurity日后哪个url处理你的登录请求
                .loginPage("/loginHtml")//用来指定自定义登录界面,不使用SpringSecurity默认登录界面  注意:一旦自定义登录页面,必须指定登录url
                //loginProcessingUrl  这个doLogin请求本身是没有的,因为我们只需要明确告诉SpringSecurity,日后只要前端发起的是一个doLogin这样的请求,
                //那SpringSecurity应该把你username和password给捕获到
                .loginProcessingUrl("/doLogin")//指定处理登录的请求url
                .and()
                .csrf().disable(); //禁止csrf 跨站请求保护
    }
}

5.1 请求参数名修改

上面的login.html用户名必须为username,密码必须为password,如果我们想要使用自定义的属性名,按照如下修改

5.1.1 修改login.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org/" lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户登录</title>
</head>
<body>
    <h1>用户登录</h1>
    <form th:action="@{/doLogin}" method="post">
        用户名:<input type="text" name="uname"> <br>
        密码:<input type="text" name="passwd"><br>
        <input type="submit" value="登录">
    </form>
</body>
</html>

5.1.2 修改配置类

package com.example.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {


    @Override
    public void configure(HttpSecurity http) throws Exception {

        //【注意事项】放行资源要放在前面,认证的放在后面
        http.authorizeRequests()
                .mvcMatchers("/index").permitAll() //代表放行index的所有请求
                .mvcMatchers("/loginHtml").permitAll() //放行loginHtml请求
                .anyRequest().authenticated()//代表其他请求需要认证
                .and()
                .formLogin()//表示其他需要认证的请求通过表单认证
                //loginPage 一旦你自定义了这个登录页面,那你必须要明确告诉SpringSecurity日后哪个url处理你的登录请求
                .loginPage("/loginHtml")//用来指定自定义登录界面,不使用SpringSecurity默认登录界面  注意:一旦自定义登录页面,必须指定登录url
                //loginProcessingUrl  这个doLogin请求本身是没有的,因为我们只需要明确告诉SpringSecurity,日后只要前端发起的是一个doLogin这样的请求,
                //那SpringSecurity应该把你username和password给捕获到
                .loginProcessingUrl("/doLogin")//指定处理登录的请求url
                .usernameParameter("uname") //指定登录界面用户名文本框的name值,如果没有指定,默认属性名必须为username
                .passwordParameter("passwd")//指定登录界面密码密码框的name值,如果没有指定,默认属性名必须为password
                .and()
                .csrf().disable(); //禁止csrf 跨站请求保护
    }
}

5.1 认证成功跳转路径

修改配置类successForwardUrl

package com.example.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {


    @Override
    public void configure(HttpSecurity http) throws Exception {

        //【注意事项】放行资源要放在前面,认证的放在后面
        http.authorizeRequests()
                .mvcMatchers("/index").permitAll() //代表放行index的所有请求
                .mvcMatchers("/loginHtml").permitAll() //放行loginHtml请求
                .anyRequest().authenticated()//代表其他请求需要认证
                .and()
                .formLogin()//表示其他需要认证的请求通过表单认证
                //loginPage 一旦你自定义了这个登录页面,那你必须要明确告诉SpringSecurity日后哪个url处理你的登录请求
                .loginPage("/loginHtml")//用来指定自定义登录界面,不使用SpringSecurity默认登录界面  注意:一旦自定义登录页面,必须指定登录url
                //loginProcessingUrl  这个doLogin请求本身是没有的,因为我们只需要明确告诉SpringSecurity,日后只要前端发起的是一个doLogin这样的请求,
                //那SpringSecurity应该把你username和password给捕获到
                .loginProcessingUrl("/doLogin")//指定处理登录的请求url
                .usernameParameter("uname") //指定登录界面用户名文本框的name值,如果没有指定,默认属性名必须为username
                .passwordParameter("passwd")//指定登录界面密码密码框的name值,如果没有指定,默认属性名必须为password
                .successForwardUrl("/index")//认证成功 forward 跳转路径
                .and()
                .csrf().disable(); //禁止csrf 跨站请求保护
    }
}

修改配置类defaultSuccessUrl

package com.example.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {


    @Override
    public void configure(HttpSecurity http) throws Exception {

        //【注意事项】放行资源要放在前面,认证的放在后面
        http.authorizeRequests()
                .mvcMatchers("/index").permitAll() //代表放行index的所有请求
                .mvcMatchers("/loginHtml").permitAll() //放行loginHtml请求
                .anyRequest().authenticated()//代表其他请求需要认证
                .and()
                .formLogin()//表示其他需要认证的请求通过表单认证
                //loginPage 一旦你自定义了这个登录页面,那你必须要明确告诉SpringSecurity日后哪个url处理你的登录请求
                .loginPage("/loginHtml")//用来指定自定义登录界面,不使用SpringSecurity默认登录界面  注意:一旦自定义登录页面,必须指定登录url
                //loginProcessingUrl  这个doLogin请求本身是没有的,因为我们只需要明确告诉SpringSecurity,日后只要前端发起的是一个doLogin这样的请求,
                //那SpringSecurity应该把你username和password给捕获到
                .loginProcessingUrl("/doLogin")//指定处理登录的请求url
                .usernameParameter("uname") //指定登录界面用户名文本框的name值,如果没有指定,默认属性名必须为username
                .passwordParameter("passwd")//指定登录界面密码密码框的name值,如果没有指定,默认属性名必须为password
//                .successForwardUrl("/index")//认证成功 forward 跳转路径,forward代表服务器内部的跳转之后,地址栏不变 始终在认证成功之后跳转到指定请求
                .defaultSuccessUrl("/index")//认证成功 之后跳转,重定向 redirect 跳转后,地址会发生改变  根据上一保存请求进行成功跳转
                .and()
                .csrf().disable(); //禁止csrf 跨站请求保护
    }
}

访问http://localhost:8080/hello,认证后

发现并没有到/index的情况,这是defaultSuccessUrl一特性,如果你想硬跳到/index,修改java defaultSuccessUrl("/index",true)即可

访问http://localhost:8080/loginHtml,认证后

defaultSuccessUrl和successForwardUrl区别

1、successForwardUrl是forward跳转,defaultSuccessUrl是重定向redirect跳转
2、successForwardUrl始终在认证成功之后跳转到指定请求,defaultSuccessUrl根据上一保存请求进行成功跳转

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: SpringSecurity自定义登录界面

本文链接: https://www.lsjlt.com/news/166939.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • SpringSecurity自定义登录界面
    为什么需要自定义登录界面? 答:因为SpringBoot整合SpringSecurity时,只需要一个依赖,无需其他配置,就可以实现认证功能。但是它的认证登录界面是固定那样的,如下图...
    99+
    2022-11-13
  • SpringSecurity自定义登录成功处理
    有时候页面跳转并不能满足我们,特别是在前后端分离开发中就不需要成功之后跳转页面。只需要给前端返回一个JSON通知登录成功还是失败与否。这个试试可以通过自定义Authenticatio...
    99+
    2022-11-13
  • FineReport中怎么自定义登录界面
    本篇文章为大家展示了FineReport中怎么自定义登录界面,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。自定义登录界面登录界面设置自定义html登录页面:命名为login.html,并保存在%FR...
    99+
    2023-06-04
  • Notification自定义界面
    前言之前在做一个手机的播放器,需要做到在通知栏显示控制播放的界面,如下:这是让服务在前台运行就可以实现的(可以参考我的前一篇文章Service在前台运行),今天我们就要实现Notification的自定义界面,当然就不实现如上图所示的了,而...
    99+
    2023-05-30
  • Win7交互式登录设置Win7登录界面提示语自定方式
    如今许多用户都是会对自身的计算机开展差异化的设置,尤其是在启动登录界面上,有一些用户想为自己的电脑上登录界面再加上提示语,可是又不知如何设置,因此小编就给各位提供了win7登录界面提示语自定方式。Win7登录界面提示语自定方式按Win+R键...
    99+
    2023-07-16
  • Android自定义videoview仿抖音界面
    本文实例为大家分享了Android自定义videoview仿抖音界面的具体代码,供大家参考,具体内容如下 1.效果图 和抖音的界面效果一模一样,而且可以自定义,需要什么页面,请自己定...
    99+
    2022-11-12
  • SpringSecurity实现自定义访问策略
    目录1.安全注释2.投票机制3.配置4.测验前言: 我们将探索一个用户共享电子表格的系统,每个电子表格的访问权限单独存储。我们已经尽可能简单地对权限存储进行了显式建模;想象一下,它在...
    99+
    2022-11-13
  • 教你自己做xp精美登录界面
        一、在你的电脑里找到logonui.exe文件,通常位于c:Windowssystem32目录中,复制两份,一份作为备份,一份用来编辑。   二、用ResHacker打开logonu...
    99+
    2023-05-23
    界面 登录 精美 自己 可以 XP 我们
  • Android自定义View仿QQ健康界面
    最近一直在学习自定义View相关的知识,今天给大家带来的是QQ健康界面的实现。先看效果图: 可以设置数字颜色,字体颜色,运动步数,运动排名,运动平均步数,虚线下方的蓝色指...
    99+
    2022-06-06
    view 界面 Android
  • jsp实现登录界面
    本文实例为大家分享了jsp实现登录界面的具体代码,供大家参考,具体内容如下 一.用户登录案例需求: 1.编写login.jsp登录页面 username & pas...
    99+
    2022-11-13
  • vue实现登录界面
    使用Vue实现简单的用户登录界面,登录成功以后查询账号用户类型进行相应的页面路由跳转,效果如下图所示: HTML部分: <div class="loginbody"> ...
    99+
    2022-11-13
  • PHP登录界面实例
    登录 来源地址:https://blog.csdn.net/m0_58464499/article/details/129665250...
    99+
    2023-09-03
    php
  • 使用SpringSecurity 进行自定义Token校验
    背景 Spring Security默认使用「用户名/密码」的方式进行登陆校验,并通过cookie的方式存留登陆信息。在一些定制化场景,比如希望单独使用token串进行部分页面的访问...
    99+
    2022-11-12
  • SpringSecurity自定义AuthenticationProvider无法@Autowire的解决
    自定义AuthenticationProvider无法@Autowire的解决 在AuthenticationProvider中使用@Autowired注入时始终报Null问题 找了...
    99+
    2022-11-12
  • 03_android集成zxing并自定义扫码界面
    03_android集成zxing并自定义扫码界面 一.zxing_core jar包生成 下载zxing sdk 下载完成和可以看到zxing...
    99+
    2022-06-06
    自定义 界面 zxing Android
  • Android自定义View制作仪表盘界面
    前言 最近我跟自定义View杠上了,甚至说有点上瘾到走火入魔了。身为菜鸟的我自然要查阅大量的资料,学习大神们的代码,这不,前两天正好在郭神在微信公众号里推送一片自定义控件的文章...
    99+
    2022-06-06
    仪表盘 view 界面 Android
  • Android ListView自定义Adapter实现仿QQ界面
    PS:listview中有一些简单使用的适配器,如:SimpleAdapter:构造方法SimpleAdapter(Context context,List<Map<String,?>> data,reStr...
    99+
    2023-05-30
    listview adapter
  • Spring Security自定义登录页面认证过程常用配置
    目录一、自定义登录页面1.编写登录页面2.修改配置类3.编写控制器二、 认证过程其他常用配置1.失败跳转1.1编写页面1.2修改表单配置1.3添加控制器方法1.4设置fail.htm...
    99+
    2022-11-13
    Spring Security登录认证配置 Spring Security登录
  • Android Studio——实现登录界面
    Android Studio——实现登录界面 在移动应用开发中,登录界面是一种常见的设计需求。通过使用Android Studio,我们可以轻松实现一个简单且美观的登录界面。本文将介绍如何使用Andr...
    99+
    2023-09-25
    android studio android ide Android
  • ThinkPhp 登录界面 中间件
    通过使用ThinkPhp中间件来实现用户登录检测。 可以参考官网https://www.kancloud.cn/manual/thinkphp6_0/1037493 启用session 在app目录下,找到middleware.php...
    99+
    2023-10-10
    中间件 Powered by 金山文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作