Springsecurity搭建自訂登入頁面

來源:互聯網
上載者:User

標籤:artifact   lan   osi   submit   erb   安全相關   uil   ppi   身分識別驗證   

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;@Configurationpublic 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層:

@Controllerpublic 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頁面

Springsecurity搭建自訂登入頁面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.