当前位置: 首页 > 知识库问答 >
问题:

Spring security中的Custom formLogin()返回(类型=禁止,状态=403)

钮实
2023-03-14

我已经使用Spring Security和Spring Web的依赖项设置了一个Spring boot应用程序。我在MemoryAuthentication()中使用了这个例子。我设置了3个html页面,并将它们放在静态文件夹中(我不使用Thymeleaf或JSP页面,只使用普通html)。

当我使用default formLogin()并运行应用程序时,我会得到spring security的默认登录页面,一旦我输入用户和密码,我就可以得到指定的页面。html和预期的一样。

当我使用定制的formLogin()运行应用程序时,我会得到403状态类型禁止:

白标错误页面

此应用程序没有 /error的显式映射,因此您将此视为后备方案。

周四12月12 10:10:14IST 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

有人也有这个问题吗<请告知,

你好Shalem

相关数据和代码:

  1. 我使用的是Spring boot 2.1.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";
    }
}

*超文本标记语言页面:


<!-- 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>

共有1个答案

太叔京
2023-03-14

如果要使用自定义登录页面,则应指定用户名和密码提交到的位置。所以,在这里换衣服,

@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();
}

之后,任何匿名用户点击任何经过身份验证的网址,然后重定向到

/麦洛金

用户输入用户名和密码后,将用户名和密码提交给

/perform_login

如果凭据有效,则重定向到

/主页

否则就回到

/麦洛金

 类似资料:
  • 我正在做一个spring boot项目,其中包括thymeleaf,spring security。当我执行以下操作时效果很好:显示产品列表、显示产品详细信息、添加新产品、更新现有产品。 但当我执行-删除产品时,会出现以下错误: 白标错误页面 此应用程序没有/error的显式映射,因此您将其视为一种回退。 18 16:59:16BDT 2019 出现意外错误(类型=禁止,状态=403)。 被禁止的

  • Spring我是新来的。我试图在我的数据库中添加一个新目标。在我添加spring security之前,它是有效的,但现在如果我单击添加新目标,我有一个问题: 出现意外错误(类型=禁止,状态=403)。被禁止的 我的goat-add.html: WebSecurity配置类: 我的控制器: 我读到这个问题可以是如果不使用csrf,但我不明白我怎么能解决它。 所有代码:https://github.

  • 问题内容: 我正在尝试制作Sitecraper。我是在本地计算机上制作的,在那儿工作得很好。当我在服务器上执行相同操作时,它显示403禁止错误。我正在使用PHP简单HTML DOM解析器 。我在服务器上收到的错误是这样的: 警告:file_get_contents(http://example.com/viewProperty.html?id=7715888)[function.file- get

  • 我正在尝试使用c#httpClient以编程方式从网站获取数据,但我无法获取数据。我提供了下面的链接https://ngodarpan.gov.in/index.php/home/statewise_ngo/5972/33/1 将有一个以表格格式显示的数据列表,如果单击任何链接,将出现一个弹出窗口,其中包含完整的详细信息,我需要通过编程为每个记录获取这些详细信息。 我已尝试通过点击以下链接每次生成

  • 出现意外错误(类型=禁止,状态=403)。访问被拒绝。当我试图从邮递员或浏览器中访问URL时,我收到了一个错误,即出现了一个意外错误(类型=禁止,状态=403)。访问被拒绝。 1) 网络安全类:- 2) 身份验证筛选器类:- 3)控制器类:- 4) 服务实现类:-

  • 我使用http://localhost:8080/auth/realms/{realm_name}/protocol/openid-connect/token endpoint创建令牌。 grant_type=client_credentials 客户端-id:------------- 客户端-secret:78296D38-CC82-4010-A817-65C283484E51 现在我想获得r