【发布时间】: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