簡體   English   中英

Spring security中的自定義formLogin()返回一個(type=Forbidden, status=403)

[英]Custom formLogin() in Spring security returns a (type=Forbidden, status=403)

我已經使用 Spring Security 和 Spring Web 的依賴項設置了一個 Spring 啟動應用程序。 我在這個例子中使用了 inMemoryAuthentication()。 我設置了 3 個 html 頁面並將它們放在靜態文件夾中(我沒有使用 Thymeleaf 或 JSP 頁面,只是純 html)。

當我使用默認的 formLogin() 並運行應用程序時,我得到了 spring security 的默認登錄頁面,一旦我輸入用戶和密碼,我就可以按預期獲得目標頁面 dash.html。

當我使用自定義的 formLogin() ,運行應用程序時,我得到狀態 403 類型禁止:

白標錯誤頁面

此應用程序沒有明確的 /error 映射,因此您將其視為后備。

12 月 12 日星期四 10:10:14 IST 2019 出現意外錯誤(類型=禁止,狀態=403)。

禁止的

我在 StackOverflow 中搜索,也在下面的鏈接中搜索,但沒有看到任何解決方案(在鏈接中它使用 Thymeleaf ,而我使用的是放在 resources/static 文件夾中的 HTML 頁面)

https://docs.spring.io/spring-security/site/docs/current/guides/html5/form-javaconfig.html#creating-a-login-view

有人也有這個問題嗎?
請指教,

問候, 沙勒姆

相關數據和代碼:

  1. 我正在使用 Spring Boot 2.1.3
  2. JAVA8
  3. 鏈接圖像中的項目文件夾布局:文件夾布局

我設置 Spring Security 代碼如下:

package com.rc1.conig;

import org.springframework.context.annotation.Bean;
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.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
            .withUser("shalem")
            .password(passwordEncoder().encode("12"))
            .roles("USER");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
            .antMatchers("/").permitAll()
            .antMatchers("/dash/**").authenticated()
            .and()
            .formLogin()
            .loginPage("/mylogin")
            .permitAll();
    }
}

- 控制器代碼:

package com.rc1.controller;

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

@Controller
public class DashController {

    @RequestMapping("/dash")
    public String getDashboard() {
        return "dash.html";
    }
}


package com.rc1.controller;

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

@Controller
public class LoginController {

    @RequestMapping("/mylogin")
    public String getLogin() {
        return "login-page.html";
    }
}

* HTML頁面:


<!-- index.html page -->

<!DOCTYPE html>
<html>
<head>
<meta charset="windows-1255">
<title>Home</title>
</head>
<body>
    <h1>Home Page</h1>
    <h3>
        <a href="http://localhost:8080/dash">dashboard</a>
    </h3>
</body>
</html>


<!-- Customized login-page.html -->

<!DOCTYPE html>
<html>
<head>
<meta charset="windows-1255">
<title>mylogin</title>
</head>
<body>
    <form action="http://localhost:8080/mylogin" method="post">
        <p>
            user: <input type="text" name="user">
        </p>
        <p>
            pass :<input type="password" name="password">
        </p>
        <button type="submit">login</button>
    </form>
</body>
</html>


<!-- dash.html page -->

<!DOCTYPE html>
<html>
<head>
<meta charset="windows-1255">
<title>Insert title here</title>
</head>
<body>
    <h1>dashboard receieved</h1>
</body>
</html>

如果要使用自定義登錄頁面,則應指定用戶名和密碼提交到的位置。 所以,只要改變這里,

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
        .antMatchers("/").permitAll()
        .antMatchers("/dash/**").authenticated()
        .and()
        .formLogin()
        .loginPage("/mylogin")
        .loginProcessingUrl("/perform_login")
        .defaultSuccessUrl("/homepage")
        .permitAll();
}

之后,任何匿名用戶點擊任何經過身份驗證的 url,然后重定向到

/mylogin

在該用戶輸入用戶名和密碼之后,該用戶名和密碼提交給

/perform_login

如果憑據有效,則重定向到

/主頁

否則回到

/mylogin

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM