SpringBoot:Imp SpringSecurity:使用用户名/密码登录时出现屏幕错误消息“用户已禁用”

Jaiswal:

我刚刚开始实现SpringSecurity,但是登录后在屏幕上显示错误消息“用户已禁用”

默认情况很好。与/ user或/ admin命中并尝试登录时,显示“ User is disable”

我正在执行以下步骤:

  1. 首先创建schema.sql(在资源文件夹下)

    CREATE TABLE USERS (
        USERNAME VARCHAR(128) PRIMARY KEY,
        PASSWORD VARCHAR(128) NOT NULL,
        ENABLED CHAR(1) CHECK (ENABLED IN ('Y','N') ) NOT NULL
    );
    
    CREATE TABLE AUTHORITIES (
        USERNAME VARCHAR(128) NOT NULL,
        AUTHORITY VARCHAR(128) NOT NULL
    );
    
    ALTER TABLE AUTHORITIES ADD CONSTRAINT AUTHORITIES_UNIQUE UNIQUE (USERNAME, AUTHORITY);
    ALTER TABLE AUTHORITIES ADD CONSTRAINT AUTHORITIES_FK1 FOREIGN KEY (USERNAME) REFERENCES USERS (USERNAME);
    
  2. 在资源文件夹下创建“ data.sql”

    insert into USERS(USERNAME,PASSWORD,ENABLED) VALUES ('user','pass','Y');
    insert into USERS(USERNAME,PASSWORD,ENABLED) VALUES ('admin','pass','Y');
    insert into AUTHORITIES (USERNAME,AUTHORITY) values ('user','ROLE_USER');
    insert into AUTHORITIES (USERNAME,AUTHORITY) values ('admin','ROLE_ADMIN');
    
  3. HomeResource.JAVA(这是一个控制器类API)

    package com.example.callCenter;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HomeResource{
    
        @GetMapping("/")
        public String home(){
            return("<h1>Welcome home page..</h1>");
        }
    
        @GetMapping("/user")
        public String user()
        {
            return ("<h1>Welcome User page</h1>");
        }
    
        @GetMapping("/admin")
        public String admin()
        {
            return("<h1>Welcome Admin page</h1>");
        }
    }
    
  4. SecurityConfiguration.java(这将启用Spring Form PAGE)

    package com.example.callCenter;
    
    import javax.sql.DataSource;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    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.core.userdetails.User;
    import org.springframework.security.crypto.password.NoOpPasswordEncoder;
    import org.springframework.security.crypto.password.PasswordEncoder;
    
    @EnableWebSecurity
    public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    
        @Autowired
        DataSource dataSource;
    
        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.jdbcAuthentication().dataSource(dataSource)
                .usersByUsernameQuery("select username,password,enabled "
                            + "from users "
                            + "where username = ?")
                .authoritiesByUsernameQuery("select username,AUTHORITY  "
                            + "from AUTHORITIES "
                            + "where username = ?");
    
            // This beloe code is default implementation with MySQL database
            // auth.jdbcAuthentication()
            //         .dataSource(dataSource)
            //         .withDefaultSchema()
            //         .withUser( 
            //                     User.withUsername("user")
            //                     .password("pass")
            //                     .roles("USER")
    
            //         )
            //         .withUser( 
            //                     User.withUsername("admin")
            //                     .password("pass")
            //                     .roles("ADMIN")
            //         );
    
        }
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().disable()
                .authorizeRequests()
                    .antMatchers("/admin").hasRole("ADMIN")
                    .antMatchers("/user").hasAnyRole("ADMIN","USER")
                    .antMatchers("/").permitAll()
                    .and().formLogin();
        }
    
        @Bean
        public PasswordEncoder getPasswordEncoder()
        {
            return NoOpPasswordEncoder.getInstance();
        }
    
    }
    
  5. CallApplication.java

    package com.example.callCenter;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class CallApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(CallCenterApplication.class, args);
        }
    
    }
    

    pom.xml:给出了所有的依赖关系,这不是问题,因为表单正在打开

    application.properties:

    spring.datasource.url=
    spring.datasource.username=
    spring.datasource.password=
    spring.datasource.driverClassName=
    
    spring.jpa.show_sql=true
    spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
    spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
    spring.jpa.hibernate.ddl-auto=update
    spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
    spring.jpa.format_sql=true
    

我添加了几乎所有详细信息,但我这边没有待解决的问题,但是现在我需要一些帮助,为什么它不允许我给用户打电话并允许我登录。

RG:

这里的问题是存储在列中的值

ENABLED CHAR(1) CHECK (ENABLED IN ('Y','N') ) NOT NULL

JdbcUserDetailsManager。loadUsersByUsername(String username)具有以下代码逻辑,用于在创建User对象时填充其属性。

                String userName = rs.getString(1);
                String password = rs.getString(2);
                boolean enabled = rs.getBoolean(3);

为了获取enabled状态,使用ResultSet.getBoolean(),相同的java文档提到以下内容

以Java编程语言中的布尔值检索此ResultSet对象的当前行中指定列的值。

如果指定的列的数据类型为CHAR或VARCHAR,并且包含“ 0”,或者数据类型为BIT,TINYINT,SMALLINT,INTEGER或BIGINT,并且包含0,则返回false值。如果指定的列的数据类型为CHAR或VARCHAR,并且包含“ 1”,或者数据类型为BIT,TINYINT,SMALLINT,INTEGER或BIGINT,并且包含1,则返回true值。

在上面的代码中,启用/禁用的正确值分别为“ 1”和“ 0”。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

IBM MQ队列列表使用用户名/密码

使用Springboot在https url中的用户名和密码

使用用户名登录或使用Cakephp 3登录

当使用正确的帐户和密码进行“ npm login”登录时,为什么仍然获得“用户名或密码错误”?

使用cURL使用用户名和密码登录到Gitlab

使用用户名和密码访问Kubernetes API

Kafka-如何在springboot yml文件中定义用户名和密码?

用户登录时登录会抛出“用户名或密码错误”错误

Microsoft信息保护SDK:使用用户名/密码登录

sqlite连接使用用户名和密码

使用用户名和密码的OpenVPN

Laravel Passport:仅使用用户名登录,无需密码

使用用户名和密码进行密钥验证

使用用户名和密码的Python github Pull

Flutter和Firebase-如何仅使用用户名和密码登录

如何在登录屏幕上禁用用户列表?(提示输入用户名和密码)Ubuntu 15.10

使用用户名和密码输入的JOptionPane

使用用户名和密码打开HTTP会话

使用用户名和密码打开的网址

如何允许客户端使用用户名和密码登录Samba共享?

使用用户名密码的Angularjs Rest端点

Amazon Cognito使用用户名和密码登录用户

当用户在登录时输入错误的用户名和密码时,它未在ROR中闪烁任何错误消息

使用用户名和密码连接到openvpn

curl 使用用户名密码和登录按钮登录网站

如何使用用户名和密码连接LDAP?

Azure AD B2C - 仅使用本地帐户登录时出现无效用户名或密码错误

需要在 C# 中使用用户名和密码创建登录窗口,但是当我输入不正确的用户名或密码时,出现错误

如何创建登录但使用用户名或电子邮件和密码