Spring 安全性 4.2.2 - 使用密码解密代理项密钥



问候堆栈溢出! 长期读者,首次撰稿人。

我正在从事一个个人项目,该项目将允许登录用户将加密的笔记存储给自己或家人。 经过大量研究,我决定实施代理密钥方法,其中每个用户在创建帐户时都会分配一个代理密钥。

此代理项密钥已加密并存储在数据库中...并在登录时使用用户的密码进行解密。 然后,生成的清除代理项密钥将用于其配置文件中的所有加密活动。

我的问题是在登录期间捕获用户的密码,同时根据本教程使用 Spring 安全性的实现:

SpringBoot MVC Spring Security Tutorial

我的 SecurityConfig 类如下所示:

/*
* (non-Javadoc)
* 
* @see org.springframework.security.config.annotation.web.configuration.
* WebSecurityConfigurerAdapter#configure(org.springframework.security.
* config.annotation.web.builders.HttpSecurity)
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/login").permitAll()
.antMatchers("/registration").permitAll().antMatchers("/accountverification/**").permitAll()
.antMatchers("/secure/**").hasAuthority("MEMBER").anyRequest().authenticated().and().formLogin()
.loginPage("/login").failureUrl("/login?error=true").defaultSuccessUrl("/secure/notes")
.usernameParameter("email").passwordParameter("password").and().logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/").and()
.exceptionHandling().accessDeniedPage("/access-denied");
}

我的登录.html页面是这样的(重要的部分):

<form th:action="@{/login}" method="POST" class="form-signin">

如果我理解正确,登录.html页面将发布到/login,这是我的 SecurityConfig 类(上面)中定义的。 登录本身效果很好。

但是,这会自动绕过我的流中解密该代理项密钥的部分。 因此,我首先尝试发布到单独的控制器,然后将该请求转发到/login。 没有骰子。

然后我尝试扩展 HandleInterceptorAdapter,但发现此时的密码仍然始终为空。 这让我想到了这篇文章:

用户详细信息 getPassword 在春季安全性 3.1 中返回空值...

。但这让我觉得很哈基。

所以,最后,我仍然想知道如何在登录屏幕发布期间获取密码以临时用于解密用户的代理密钥......然后当解密完成并且登录完成时...摧毁它。

任何帮助不胜感激!

我知道这会发生...一旦我在StackOverflow上发布一些花哨的问题,我就会弄清楚。

我不确定这是最优雅的解决方案,但它有效。 我当然对替代方案持开放态度。

我将此方法添加到我的登录控制器:

/**
* @return
*/
@RequestMapping(value = { "/loginProcessor" }, method = RequestMethod.POST)
public String loginProcessor(@ModelAttribute Login login) {
System.out.println("PASSWORD: " + login.getPassword());
return "forward:/login";
}

然后我调整了登录.html以发布到它,而不是/login:

<form th:action="@{/loginProcessor}" method="POST" class="form-signin">

我更新了我的 SecurityConfig 以适应两条路径:

/*
* (non-Javadoc)
* 
* @see org.springframework.security.config.annotation.web.configuration.
* WebSecurityConfigurerAdapter#configure(org.springframework.security.
* config.annotation.web.builders.HttpSecurity)
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/loginProcessor").permitAll()
.antMatchers("/login").permitAll().antMatchers("/registration").permitAll()
.antMatchers("/accountverification/**").permitAll().antMatchers("/secure/**").hasAuthority("MEMBER")
.anyRequest().authenticated().and().formLogin().loginPage("/login").failureUrl("/login?error=true")
.defaultSuccessUrl("/secure/notes").usernameParameter("email").passwordParameter("password").and()
.logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/").and()
.exceptionHandling().accessDeniedPage("/access-denied");
}

等等,瞧! 打印了密码,我已登录并重定向到我的个人资料页面。 有没有更好的方法?

最新更新