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

Spring Boot Security自定义登录页面

浦出野
2023-03-14

我想知道我应该如何创建我自己的自定义登录页面?我尝试了https://docs.spring.io/spring-security/site/docs/current/guides/html5/form-javaconfig.html和http://www.baeldung.com/spring-mvc-tutorial#configviews等指南,但没有任何效果,因为我似乎无法使webmvcconfig类工作。我一直有404个错误。这是我当前的文件

安全配置类

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{

    /**
     * 
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests().anyRequest().authenticated().and()
            .formLogin().loginPage("/login").permitAll();
        http.authorizeRequests().antMatchers("/stats").hasRole("ADMIN")
                .and().formLogin().loginPage("/login");
        //everything else ADMIN and USERS can see
        http.authorizeRequests().antMatchers("/").hasAnyRole("ADMIN","USER")
                .and().formLogin().loginPage("/login");     
    }

    @Autowired
    DataSource dataSource;

    @Autowired
    public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication().dataSource(dataSource)
            .passwordEncoder(passwordEncoder());
    }

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

login.jsp文件

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Insert title here</title>
    </head>
    <body>
        <h1>HERE!!!!</h1>
        <form action="${contextRoot}/login" method="post">
            <input type="text" name="username" value="username"/>
            <input type="password" name="password"/>
            <input type="submit" value="Login"/> 
        </form>
    </body>
</html>

和Web Mvc配置类

@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/login").setViewName("login");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }
}

共有1个答案

阴永逸
2023-03-14

您可以尝试这样做:以下示例在JSF和Spring中https://www.codeNotfound.com/jsf-primeFaces-spring-security-example.html

 类似资料:
  • 请参考一些wordpress post登录插件。我有3页的网站主页,关于正常工作和工作安全,我想显示工作安全页面,如果用户登录有电子邮件/只是虚拟id,我可以生成。找不到此登录模块的插件。带有自定义页面的自定义菜单 使用这个脚本,我可以创建菜单,但不能重定向单独的页面 函数my_wp_nav_menu_args($args=''){ 如果(用户是否已登录){ }否则{ } } 添加过滤器(“wp\

  • 我在我的JSF应用程序中使用容器管理的安全性,因此我有一个低于默认设置的登录页面。 现在在登录页面中,我想在登录按钮旁边添加一个按钮,以允许用户注册。 但我该如何将其转发到我的登记簿。我的注册按钮的xhtml页面?我用以下代码进行了尝试: 我也尝试将表单更改为h: form,以便我可以使用p:命令按钮,但正如我注意到的,我的登录页面不工作,当我单击提交按钮时什么也没有发生。 如何实现我想做的事?

  • 而且,再一次,即使键入正确的凭据,正如我在“userDetailsService”方法中指定的那样,我也无法登录,但这次我终于可以在控制台上看到错误消息: 显然我走了,但没有离开这个地方,因为现在我不知道我的代码有什么问题...

  • 我使用jsf servlet并创建了自定义登录页面,使用了带有动作j_spring_security_check的表单,方法POST,使用了j_username和j_password。当我单击登录按钮时,我遇到的问题不是重定向到页面,而是在Chrome网络列中显示了带有302状态代码的post方法j_spring_security_check,我是否使用了Spring安全的正确方式? 登录。jsf

  • 问题内容: 我有一个网站,有一个用户系统。我想将wordpress的用户系统集成到该网站的系统中,但是我仍然想使用该网站的注册/登录页面。我不希望任何人都能使用Wordpress的登录或注册表格进行登录或注册。相反,当他们尝试访问Wordpress中的登录/注册页面时,我希望这些页面将它们重定向到我自己的登录/注册页面。 有什么办法吗?我已经尝试过Google,但是我所能找到的只是在用户登录或注册

  • 我试图在我的Spring项目上实现一个自定义登录页面,但是当我运行该项目时,我得到以下错误: 有人知道是什么导致了这个错误吗??