zoukankan      html  css  js  c++  java
  • Springsecurity搭建自定义登录页面

    1.springSecurity的搭建

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

    image.png

    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.项目启动

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

    image.png

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

    image.png

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

  • 相关阅读:
    IOS客户端Coding项目记录(二)
    IOS客户端Coding项目记录(一)
    IOS开发基础知识--碎片7
    图解域域树域林根域的含义
    Windows server 2012公用网络修改为专用网络
    Windows2012R2版本区别
    VMWare:vSphere6 企业版参考序列号
    Oracle快速测试连接是否成功
    Brocade300 commands
    也谈免拆机破解中兴B860av1.1(解决不能安装软件/解决遥控)
  • 原文地址:https://www.cnblogs.com/charlypage/p/9320515.html
Copyright © 2011-2022 走看看