【问题标题】:Spring Security 4.2.2 - Using Password To Decrypt Surrogate KeySpring Security 4.2.2 - 使用密码解密代理密钥
【发布时间】:2017-06-09 03:04:45
【问题描述】:

问候 StackOverflow!长期读者,第一次投稿。

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

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

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

Spring Boot 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");
}

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

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

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

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

然后我尝试扩展HandleInterceptorAdapter,但发现此时密码仍然为空。这导致我写了这篇文章:

UserDetails getPassword returns null in spring security 3.1...

...但这让我觉得很老套。

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

感谢任何帮助!

【问题讨论】:

    标签: java spring encryption spring-security


    【解决方案1】:

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

    我不确定这是最优雅的解决方案,但它确实有效。我当然愿意接受替代方案。

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

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

    然后我调整 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");
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多