【问题标题】:Why Spring Security InMemoryAuthentication do not authenticate me为什么 Spring Security InMemoryAuthentication 不对我进行身份验证
【发布时间】:2017-07-22 00:44:55
【问题描述】:

我的项目中只有以下三个类和一个视图文件。没有配置文件。我尝试在 spring mvc 应用程序上配置 spring security。

当我将它作为 java 应用程序运行并在浏览器中访问它时,它会要求输入密码。

我在相应的表单字段中输入用户名和密码,但它没有进行身份验证。

我检查了日志,发现这一行

Using default security password: 75a836df-d369-4600-aad2-a50567ebd283

当 ii 使用它时,它成功让我登录。

我无法理解这一点。谁能给我解释一下?我失踪了吗 什么?

package hello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");


    }
}

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);

    }

}

package hello;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class GreetingController {

    @RequestMapping("/greeting")
    private String greeting(@RequestParam(value="name", defaultValue="World", required=false) String name, Model model) {


        model.addAttribute("name", name);

        return "greeting";

    }
}

我在问为什么它没有使用用户名登录我 “用户”和密码为“密码”

【问题讨论】:

  • 你的问题很不清楚。你真的是说你使用了“使用”吗?在这种情况下,问题在于默认用户名是“user”。如果这不是你的意思,那么你所说的“用这个”登录到底是什么意思?我猜你的问题可能是你的configureGlobal 方法是private 而不是public
  • 另外,供将来参考:在似乎没有预期效果的方法中放置一个输出语句或调试断点,以查看它们是否被调用。跨度>
  • 对不起,这是一个错字,我的意思是我使用用户名和密码作为登录详细信息,就像它们在身份验证代码中一样。
  • 我在问为什么它没有使用用户名作为“用户”和密码作为“密码”登录
  • 我已经公开了该方法,但它仍然没有让我登录。但问题是为什么当我输入从日志中获取的密码时它让我登录了,日志中的行就是我在我的问题中显示了密码。 @chrylis 你也可以看到我上面的 cmets。

标签: java spring spring-mvc spring-security


【解决方案1】:

我已经解决了这个问题。我必须改变

@EnableWebSecurity

@Configuration
@EnableWebMvcSecurity

SecurityConfig 类上。

实际上,我正在学习 Spring 框架,我发现有这么多的选择,有很多不同的方法可用并在互联网上讨论。所以会出现这样的问题。

【讨论】:

  • 还要注意@EnableWebSecurity非常新的。
  • 这就是为什么您需要解释 Spring 版本并且可能更深入地解释您的配置的原因。无论如何,很好的解决方案
【解决方案2】:

您应该在 WebSecurityConfigurerAdapter 的实现中覆盖方法 configure

    @Override
    protected void configure(AuthenticationManagerBuilder authManagerBuilder) throws Exception {
        authManagerBuilder.inMemoryAuthentication()
                .withUser("user").password("password").roles("USER");
    }

你所做的是实现了方法configureGlobal,它在WebSecurityConfigurerAdapter中没有出现,Spring对这个方法一无所知。

【讨论】:

  • Spring Security 会自动通过@Autowired void 方法将AuthenticationManagerBuilder 作为参数发送。
  • 好的,它有效,但这种方法仍然看起来很奇怪。在这种情况下使用 WebSecurityConfigurerAdapter 的原因是什么?
  • 我不太确定。 Spring Security 的构建方式有很多对我来说没有意义,我使用它的唯一原因是它已经与许多其他工具集成。
【解决方案3】:

您可以使用以下代码,在您的代码配置方法中缺少

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
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;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true,prePostEnabled = true)
public class ApplicationSecurity extends WebSecurityConfigurerAdapter {


     @Autowired
        public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
              auth.inMemoryAuthentication().withUser("admin").password("khan1234").roles("ADMIN");
              auth.inMemoryAuthentication().withUser("vaquar").password("khan1234").roles("USER");

        }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/api/**").hasAnyRole("ADMIN","USER").and().httpBasic();//.and().headers().disable();
        //.and().formLogin();


    }
}

【讨论】:

    猜你喜欢
    • 2014-11-23
    • 2014-02-01
    • 2014-01-28
    • 2016-09-13
    • 2013-03-02
    • 2012-11-27
    • 2019-02-07
    • 2013-12-16
    相关资源
    最近更新 更多