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

Spring Security/Java配置-无法使用现有用户登录

涂溪叠
2023-03-14
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

//    @Autowired
//    AuthProvider provider;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("user1").password("").roles("USER")
                .and()
                .withUser("user2").password("").roles("USER")
                .and()
                .withUser("admin").password("1").roles("ADMIN");
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf()
                    .disable()
                .authorizeRequests()
                    .anyRequest().authenticated()
                    .and()
                .formLogin()
                    .loginPage("/login")
                    .permitAll();

    }


//    @Override
//    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        auth.authenticationProvider(provider);
//    }
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
${message}
<br>
<form method="post" action="/login">
    <input type="text" name="login"/>
    <input type="text" name="pass"/>
    <input type="submit" value="enter"/>
</form>
</body>
</html>

共有1个答案

东门令
2023-03-14

在您的代码

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("user1").password("").roles("USER")
                .and()
                .withUser("user2").password("").roles("USER")
                .and()
                .withUser("admin").password("1").roles("ADMIN");
}

@autowired替换为@override

并在以下[1]:https://www.baeldung.com/spring-security-login遵循此实践

... auth.inMemoryAuthentication()
      .withUser("user1").password(passwordEncoder().encode("user1Pass")).roles("USER")
      .and() ...
    @Bean
        public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
 类似资料:
  • 目前,我正在使用以下内容将用户登录到我的应用程序中。然而,我想使用一个角函数来实际执行登录。为此,我想创建一个Rest网络服务来进行身份验证,但是我在SO上看到的所有示例都使用我认为被贬低的用户。我还希望该服务返回有关用户的信息。 我要问的是如何将MyUserDetailsService更改为用作登录的restful服务,或者如何创建一个可用于登录的服务,该服务将在登录后返回用户对象。 这是我的a

  • 当我运行我的应用程序时,项目的常规选项卡的签名部分出现了两个错误。一个说,“操作无法完成。无法使用帐户登录 ________" 而另一个说“找不到 ________ 的配置文件”。它提到Xcode找不到配置文件。这是什么意思,我该如何修复?

  • 本文向大家介绍Springboot+SpringSecurity+JWT实现用户登录和权限认证示例,包括了Springboot+SpringSecurity+JWT实现用户登录和权限认证示例的使用技巧和注意事项,需要的朋友参考一下 如今,互联网项目对于安全的要求越来越严格,这就是对后端开发提出了更多的要求,目前比较成熟的几种大家比较熟悉的模式,像RBAC 基于角色权限的验证,shiro框架专门用于

  • 问题内容: 使用此命令 我尝试登录: 错误是: 我以root用户身份执行此操作,并且确实尝试刷新特权。 我尝试了无数用户,但这似乎行不通。我可以创建没有密码的用户,并且登录有效。命令行和从phpmyadmin 还要检查用户是否在mysql.user中。 布赖恩表演的表演赠款: 问题答案: 您可能会遇到这种永久性的MySQL问题,其中user表中的默认用户之一是,这最终会在表的后面拒绝所有用户。我会

  • 我正在尝试使用现有的虚拟环境设置让PyCharm运行。我已将我的PyCharm项目指向以下路径中现有虚拟环境中的python解释器~/.虚拟环境/myproj/ 文件- 该项目运行良好,但编辑器在作为virtualenv的一部分安装的包上仍然亮起红色。 知道我错过了什么吗?

  • 问题内容: 我没有前端。我希望用户将我的API(例如,(邮递员))与基本身份验证标头(电子邮件:密码)一起使用,然后使用此标头可以从firebase获取用户数据。 但是我找不到验证实际用户的方法吗? 我已经使用以下命令正确连接到数据库 我试图研究这些方法: 但是我看不到从服务器登录用户的选项… 从nodejs服务器似乎非常容易…例如 java /中是否存在此方法?如何在Java中针对服务器且没有客