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")相对应,原因为:

由于security是由UsernamePasswordAuthenticationFilter这个类定义登录的,里面默认是/login路径,我们要让他用我们的/authentication/form路径,就需要配置.loginProcessingUrl("/authentication/form")

3.项目启动

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

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

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

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

  1. Spring MVC 项目搭建 -4- spring security-添加自定义登录页面

    Spring MVC 项目搭建 -4- spring security-添加自定义登录页面 修改配置文件 <!--spring-sample-security.xml--> <!-- ...

  2. Spring Security 自定义登录页面

    SpringMVC + Spring Security,自定义登录页面登录验证 学习参考:http://www.mkyong.com/spring-security/spring-security-f ...

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

    如果想将动态管理资源与自定义登录页面一起使用,最简单的办法就是在数据库中将登录页面对应的权限设置为IS_AUTHENTICATED_ANONYMOUSLY. 因此在数据库中添加一条资源信息. INSE ...

  4. 为SharePoint 2010中的FBA创建自定义登录页面

    SharePoint 2010中默认的FBA登录页面非常简单,只提供了一个Asp.Net的Login控件,让用户输入用户名和密码.在大多数情况下,我们需要定制这个页面以满足一些安全需求,比如为登录页面 ...

  5. CAS 4.0.x 自定义登录页面

    版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[-] CAS默认登录页面 复制一个新的页面管理页面 修改页面引用 修改casproperties 修改casLoginViewjs ...

  6. Drupal中自定义登录页面

    通过覆写template定义新的user_login表单来为自定义登录页面.方法: 1.  本站使用的主题是Rorty.来到\sites\all\themes\rorty,打开template.php ...

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

    一.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  8. (二)spring Security 自定义登录页面与校验用户

    文章目录 配置 security 配置下 MVC 自定义登录页面 自定义一个登陆成功欢迎页面 效果图 小结: 使用 Spring Boot 的快速创建项目功能,勾选上本篇博客需要的功能:web,sec ...

  9. SpringSecurity 3.2入门(5)自定义登录页面

    增加spring-security.xml文件配置如下 <!-- 配置SpringSecurity的http安全服务 --> <security:http auto-config=& ...

随机推荐

  1. 排序:快速排序Quick Sort

    原理,通过一趟扫描将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序 ...

  2. fakeapp, faceswap, deepfacelab等deepfakes换脸程序的简单对比

    https://deepfakes.com.cn/index.php/95.html https://www.cnblogs.com/zackstang/p/9011753.html

  3. IntelliJ IDEA 2016.1.3(64) license server 与汉化

    license server:http://idea.iteblog.com/key.php 汉化:将resources_cn.jar 复制到安装IDEA安装目录下的lib文件夹中.重新打开即可. r ...

  4. 用 Servlet 进行上载的原理和实现

    Servlet 是用 Java 编写的.协议和平台都独立的服务器端组件,使用请求/响应的模式,提供了一个基于 Java 的服务器解决方案.使用 Servlet 可以方便地处理在 HTML 页面表单中提 ...

  5. java中的四种代码块

    一.普通代码块 直接在一个方法中出现的{}就称为普通代码块,例子程序如下: public class CodeDemo01{ public static void main(String[] args ...

  6. VS2017安装时自动退出

    电脑重装系统后 win10,安装VS2017,一直不成功:保持一定时间后就自动退出,而没有跳到具体安装选择项界面.重复了好几次,并且电脑也重启还是这样. 最后在系统盘下面找到文件夹:C:\Progra ...

  7. ffmpeg学习(三)——ffmpeg+SDL2 实现简单播放器

    本篇实现基于ffmpeg动态库用测试程序播放本地文件和RTSP视频流. 参考文章:http://blog.csdn.net/leixiaohua1020/article/details/8652605 ...

  8. 基于ASP.NET生成条形码(code128)

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Dr ...

  9. IIS发布好的网页突然不显示图片了

    按以下步骤把地址加到ie的本地intranet就好了

  10. java简单正则验证手机号

    import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @Title:Tadesfza * @Description ...