【发布时间】:2015-08-12 23:29:30
【问题描述】:
一个应用程序当前使用 Spring Security,其用户名和密码由 Spring 自动生成的登录表单处理。用户名是有效的电子邮件地址。
我还有其他代码可以向用户发送一封电子邮件,其中包含 url 中的会话 ID 以验证电子邮件地址。这还会向用户发送一条带有自定义密码的短信,以验证他们的电话号码。注册站点、验证电子邮件和验证电话号码的步骤需要几个控制器方法和步骤。
是否有一种快速简便的方法可以将代码添加到现有的控制器方法中,该方法只是将用户声明为已登录?这将使用现有的 spring 安全配置,并通过声明用户来简单地增加已登录。
例如,在控制器方法中:
//confirmed email
//confirmed phone number
//confirmed password
loggedin = true;
澄清:
每次用户登录都需要发送和验证确认文本密码和确认电子邮件。如果无法通过简单的编程语句登录某人,我至少可以用一个简单的程序语句改变用户的角色?因此,他们使用用户名和密码登录到有限的条件状态,并被分配“StepOneRole”。然后他们验证发送给他们的 pin 码并获得“StepTwoRole”。然后他们验证发送给他们的电子邮件链接,并将他们的角色更改为“FullUser”,在其中他们可以实际使用网站的受保护部分。
我试图避免增加不必要的复杂性,但用户需要每次验证 n 个因素。
另外,请注意我的SecurityConfig.java 如下,并使用UserDetailService,在以下答案之一中提到:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/user-home")
.usernameParameter("j_username")
.passwordParameter("j_password")
.loginProcessingUrl("/j_spring_security_check")
.failureUrl("/login")
.permitAll()
.and()
.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/login")
.and()
.authorizeRequests()
.antMatchers("/user-home").hasAuthority("USER")
.antMatchers("/j_spring_security_check").permitAll()
.and()
.userDetailsService(userDetailsService);
}
}
【问题讨论】:
标签: java spring spring-mvc spring-security