Springsecurity搭建自定义登录页面

1.springSecurity的搭建

新建一个springboot的web项目,我这边只选中了web,建立后如下:

pom依赖:

<!-- https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-jasper -->
        <!--配置支持jsp-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <version>8.5.12</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!--添加static和templates的依赖-->
        <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.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <!-- 由于我使用的spring boot所以我是引入spring-boot-starter-security而且我使用了spring io所以不需要填写依赖的版本号 -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

以上的jsp依赖如果用不上可以不加哦

2.编写SecurityConfiguration来继承WebSecurityConfigurerAdapter

WebSecurityConfigurerAdapter是security中浏览器登录设置的主类 这里我们继承后重写以下的三个方法:

  • HttpSecurity(HTTP请求安全处理)
  • AuthenticationManagerBuilder(身份验证管理生成器)
  • WebSecurity(WEB安全)
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        super.configure(auth);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/hello","/login.html").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                //指定登录页的路径
                .loginPage("/hello")
                //指定自定义form表单请求的路径
                .loginProcessingUrl("/authentication/form")
                .failureUrl("/login?error")
                .defaultSuccessUrl("/success")
                //必须允许所有用户访问我们的登录页(例如未验证的用户,否则验证流程就会进入死循环)
                //这个formLogin().permitAll()方法允许所有用户基于表单登录访问/login这个page。
                .permitAll();
                //默认都会产生一个hiden标签 里面有安全相关的验证 防止请求伪造 这边我们暂时不需要 可禁用掉
                http .csrf().disable();

    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        super.configure(web);
    }

}

这边我们指定的登录页的访问方法为/Hello的方法,这边我们来编写这个Controller层:

@Controller
public class LoginController {

    @RequestMapping("/hello")
    public String hello() {
        //这边我们,默认是返到templates下的login.html
        return "login";
    }
}

login.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>第一个HTML页面</title>
</head>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
自定义表单验证:
<!--<form name="f" action="/login" method="post">-->
    <form name="f" action="/authentication/form" method="post">
<br/>
用户名:
<input type="text" name="username" placeholder="name"><br/>
密码:
<input type="password" name="password" placeholder="password"><br/>
<input name="submit" type="submit" value="提交">
</form>
</body>
</html>

这里值的注意的是表单的用户名name和password输入框的name=""要和security里面的验证的对应:

name="username";name="password",否则无法识别,另外action="/authentication/form"要与.loginProcessingUrl("/authentication/form")相对应,至于路径不写为/authentication/form(写其他路径会报错还没深入研究,知道的小伙伴可以给我留言).

3.项目启动

我们现在启动项目 无论进入哪个网址都会被拦截返回到登录页面,如下所示:

这时我们用户名:user(默认) password:会在启动时候生成 如下:

这个时候我们登录就成功了 ,否则不正确会返回到error页面

原文地址:https://www.cnblogs.com/charlypage/p/9320515.html

时间: 2024-08-03 13:07:36

Springsecurity搭建自定义登录页面的相关文章

spring-security 个性化用户认证流程——自定义登录页面(可配置)

1.定义自己的登录页面我们需要根据自己的业务系统构建自己的登录页面以及登录成功.失败处理在spring security提供给我的登录页面中,只有用户名.密码框,而自带的登录成功页面是空白页面(可以重定向之前请求的路径中),而登录失败时也只是提示用户被锁定.过期等信息. 在实际的开发中,则需要更精细力度的登录控制,记录错误的日志(错误的次数等) 2.自定义登录页面 配置登录页面的路径在BrowserSecurityConfig类中配置登录页面的路径 http.formLogin() .login

CAS 4.0.x 自定义登录页面

版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] CAS默认登录页面 复制一个新的页面管理页面 修改页面引用 修改casproperties 修改casLoginViewjsp页面 用过 CAS 的人都知道 CAS-Server端是单独部署的,作为一个纯粹的认证中心.在用户每次登录时,都需要进入CAS-Server的登录页填写用户名和密码登录,但是如果存在多个子应用系统时,它们可能都有相应风格的登录页面,我们希望直接在子系统中登录成功,而不是每次都要跳转到CAS的登录页去登

SharePoint2013自定义登录页面

SharePoint默认创建的WebApplication是基于windows的身份验证模式,以弹出窗口方式进行身份输入登录.这种方式可能对于国内的企业用户来说感觉不是很友好.一般而言,我们会把这种登录模式变更为FBA(Forms Based Authentication)方式或混合模式,此文章就是告诉大家如何在WebApplication的FBA方式下自定义登录页面. 本文章中不去涉及FBA的配置过程,这部分的内容我会在另一篇文章中单独摘写. 我们假设已经将一个WebApplication配置

spring security动态管理资源结合自定义登录页面

如果想将动态管理资源与自定义登录页面一起使用,最简单的办法就是在数据库中将登录页面对应的权限设置为IS_AUTHENTICATED_ANONYMOUSLY. 因此在数据库中添加一条资源信息. INSERT INTO RESC VALUES(1,'','URL','/login.jsp*',1,'') 这里的/login.jsp*就是我们自定义登录页面的地址. 然后为匿名用户添加一条角色信息: INSERT INTO ROLE VALUES(3,'IS_AUTHENTICATED_ANONYMOU

第 8 章 动态管理资源结合自定义登录页面

转载:http://www.mossle.com/docs/auth/html/ch008-db-login.html 第 8 章 动态管理资源结合自定义登录页面 如果想将动态管理资源与自定义登录页面一起使用,最简单的办法就是在数据库中将登录页面对应的权限设置为IS_AUTHENTICATED_ANONYMOUSLY. 因此在数据库中添加一条资源信息. INSERT INTO RESC VALUES(1,'','URL','/login.jsp*',1,'') 这里的/login.jsp*就是我

MVC4.0 使用Form认证,自定义登录页面路径Account/Login

使用MVC4.0的时候,一般遇到会员登录.注册功能,我们都会使用Form认证,给需要身份验证的Action进行授权(需要登录后才能访问的Action添加[Authorize]属性标签),登录.注册的时候给用户添加票据信息,以便可以访问需要身份验证的Action操作或者视图 同时在web.config中我们会看到这样的配置代码,当我们修改loginUrl的值时,会发现当我们未被授权但要访问需要身份验证的视图时,依然会被强制返回Account/Login页面 <authentication mode

CAS自定义登录页面

首先,在我们的子系统中应该有一个登录页面,通过输入用户名和密码提交至cas认证中心.不过前提是先要获取到 login tickt id. 也就是说当用户第一次进入子系统的登录页面时,在该页面中会通过js跳转到 cas/login 中的获取login ticket. 在 cas/login 的 flow 中先会判断请求的参数中是否包含了 get-lt 的参数.  (1) 在cas的 login flow 中加入 ProvideLoginTicketAction 的流,主要用于判断该请求是否是来获取

Azure B2C登录,react-web端实现,自定义登录页面ui

import React, { Component } from 'react'; import Particles from 'react-particles-js'; import { Form, Button } from 'antd'; import { connect } from 'react-redux'; import { setUserInfo } from '@/redux/actions/userInfo'; import '@/assets/css/login'; imp

FineReport中如何自定义登录界面

在登录平台时,不希望使用FR默认的内置登录界面,想通过自定义登录界面实现登录操作,内置登录界面如下图: 登录界面,获取到用户名和密码的值,发送到报表系统,报表服务带着这两个参数访问认证地址进行认证. 自定义登录界面 登录界面设置 自定义html登录页面:命名为login.html,并保存在%FR_HOME%\WebReport下,代码如下: <html>   <head>  <meta http-equiv="Content-Type" content=&