如何使用登录页面的Java配置配置Spring MVC HttpSecurity
我正在使用Java配置而不是xml构建一个SpringMVC应用程序,使用Eclipse、Maven和SpringWebMVC4.2.4版和SpringSecurity4.0.3版。我让它在Tomcat 7上运行。 我可以从一个jsp导航到另一个jsp,因此@RequestMappings是正确的(它们列在下面的configure()方法中)。我用log4j设置了日志记录,记录了所有可能的内容,因此我可以看到正在调用我的配置和控制器。启动期间,日志文件显示正在设置的映射: …RequestMappingHandlerMapping-映射的“{[/login],方法=[GET]}”。。。 …RequestMappingHandlerMapping-映射的“{[/login],方法=[POST]}” 我的问题是,当登录屏幕被提交时,它没有发送到LoginController类中的正确方法,而是一直发送到为GET请求注释的“init”方法 这里是SecurityConfig:如何使用登录页面的Java配置配置Spring MVC HttpSecurity,spring,spring-mvc,spring-security,Spring,Spring Mvc,Spring Security,我正在使用Java配置而不是xml构建一个SpringMVC应用程序,使用Eclipse、Maven和SpringWebMVC4.2.4版和SpringSecurity4.0.3版。我让它在Tomcat 7上运行。 我可以从一个jsp导航到另一个jsp,因此@RequestMappings是正确的(它们列在下面的configure()方法中)。我用log4j设置了日志记录,记录了所有可能的内容,因此我可以看到正在调用我的配置和控制器。启动期间,日志文件显示正在设置的映射: …RequestMap
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/","/register","/about","/home","/demo").permitAll()
//.loginProcessingUrl("/login")
.antMatchers(HttpMethod.POST,"/login").permitAll()
.anyRequest().authenticated()
.and().formLogin().permitAll().loginPage("/login")
.and().logout().logoutUrl("/logout").invalidateHttpSession(true).logoutSuccessUrl("/home");
}
}
当//.loginProcessingUrl(“/login”)未注释时,将显示自动生成的Spring登录表单,我可以登录!因此,它适用于默认表单,但不适用于我的表单
LoginController.java如下所示:
@Controller
public class LoginController {
private Logger logger = Logger.getLogger(LoginController.class);
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String init(Locale locale, Model model) {
logger.info("LoginController login INIT!");
return "login";
}
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(@ModelAttribute LoginDTO loginObject, Locale locale, Model model) {
logger.info("LoginController login POST!");
return "home";
}
}
提交Spring默认登录页面时,它不会映射到我的LoginController。当提交my login.jsp时,请求将转到init()方法,而不是映射到POST的login()方法
我要使用的自定义login.jsp的一段代码,而不是默认的jsp:
用户名
密码
登录
该框架正在登录页面上添加CSRF令牌,我可以在浏览器上看到它,因此这似乎是可行的,但我不确定这是否重要
我只是在学习Spring,一直在到处寻找关于Spring安全性如何工作和配置的深入解释,特别是包含所有链接方法的http对象。如果有人能告诉我最新的Spring安全性的参考资料,我将不胜感激,或者让我知道我的代码需要什么。据我所知,您应该从控制器中删除
@RequestMapping(value=“/login”,method=RequestMethod.POST)
,并按如下方式更改安全配置:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/", "/register", "/about", "/home", "/demo").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.loginProcessingUrl("/login")
.permitAll()
.and()
.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/home")
.invalidateHttpSession(true);
}
}
Spring将使用Spring安全过滤器自动处理您的POST请求,并在必要时将您重定向到登录表单。请确保您的登录字段名正确命名为“用户名”和“密码”:
用户名
密码
登录
有关更多信息,请参阅。是的,这很有效!我现在可以登录并查看受保护的页面。但我不能注销,所以我需要处理这部分。下一步是添加JDBC登录,但您帮助我克服了这个障碍。谢谢
<form:form action="${loginProcessingUrl}" method="post">
<p>
<label for="username">Username</label>
<input type="text" id="username" name="username"/>
</p>
<p>
<label for="password">Password</label>
<input type="password" id="password" name="password"/>
</p>
<div>
<button type="submit">Log in</button>
</div>
</form:form>