Spring security 如何使用spring boot安全性获取用户名和输入的密码而不进行编码

Spring security 如何使用spring boot安全性获取用户名和输入的密码而不进行编码,spring-security,spring-boot,Spring Security,Spring Boot,我已经用spring boot security实现了安全层,并使用MD5加密机制对提供的密码进行了编码。它的工作如预期的那样完美,但我需要获得用户在DAO或服务层中输入的用户名和原始密码。下面是我使用的代码 @Autowired UserDao userDao; @Autowired @Qualifier("userDetailsService") UserDetailsService userDetailsService; @Autowired private RESTAuthentic

我已经用spring boot security实现了安全层,并使用MD5加密机制对提供的密码进行了编码。它的工作如预期的那样完美,但我需要获得用户在DAO或服务层中输入的用户名和原始密码。下面是我使用的代码

@Autowired
UserDao userDao;

@Autowired
@Qualifier("userDetailsService")
UserDetailsService userDetailsService;

@Autowired
private RESTAuthenticationEntryPoint authenticationEntryPoint;

@Autowired
private RESTAuthenticationFailureHandler authenticationFailureHandler;
@Autowired
private RESTAuthenticationSuccessHandler authenticationSuccessHandler;

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/css/**", "/fonts/**", "/images/**");
}


/**
 * Security implementation to access the services
 */
@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests().antMatchers("/", "/index.html","/home.html","/page/*","/home/*", "/login.html","/login","/cms/createPhoneNo").permitAll();
    http.authorizeRequests().anyRequest().fullyAuthenticated().and().httpBasic().and().csrf().disable();
    http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint);
    http.formLogin().loginProcessingUrl("/login/authenticate").successHandler(authenticationSuccessHandler);
    http.formLogin().failureHandler(authenticationFailureHandler);
    http.logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).invalidateHttpSession(true);
    http.exceptionHandling().accessDeniedHandler(accessDeniedHandler());

    // CSRF tokens handling
    http.addFilterAfter(new CsrfTokenResponseHeaderBindingFilter(), CsrfFilter.class);
}

/**
 * Configures the authentication manager bean which processes authentication
 * requests.
 */
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    // Dao based authentication
    auth.userDetailsService(userDetailsService).passwordEncoder(new Md5PasswordEncoder());
}

private AccessDeniedHandler accessDeniedHandler() {
    return new AccessDeniedHandler() {

        @Override
        public void handle(HttpServletRequest request, HttpServletResponse response,
                AccessDeniedException accessDeniedException) throws IOException, ServletException {
            response.getWriter().append("Access denied");
            response.setStatus(403);
        }
    };
}

/**
 * This bean is load the user specific data when form login is used.
 */
@Bean
public UserDetailsService userDetailsService() {
    return new MyCustomUserDetailsService(userDao);
}
}

有人能帮我实现这个场景吗


谢谢,

添加到您的
配置(AuthenticationManagerBuilder)
方法:

auth.credentials(false);
然后,您可以通过以下方式获取当前用户的用户名和密码:

String username=SecurityContextHolder.getContext().getAuthentication().getName();
对象rawPassword=SecurityContextHolder.getContext().getAuthentication().getCredentials();

在我提供答案之前,必须警告:

以明文形式存储用户密码是非常危险的。任何人 有权访问数据库有权访问用户密码,这意味着他们可以 在应用程序中模拟用户。用户也倾向于重用 密码,使该用户暴露在其他系统(其 电子邮件、他们的facebook等)

别挡道了,哦,等等,再来一个:

MD5是一个非常弱的单向散列。这要视情况而定 生成给定哈希的纯文本很容易被发现。 考虑切换到更强大的东西,比如SHA256,Scrypt, Bcrypt或PBKDF2


好的,这些都做好了。使用
org.springframework.security.authentication.encoding.PlaintextPasswordEncoder
并在DAO中按需应用MD5(或更好的轻推轻推)散列。

您无法“解码”MD5。Md5是(非常弱的)单向散列。我可以问你为什么要原始密码吗?散列的要点是不要有这个。此外,考虑至少使用SHI-256或BCRIPT或ScRyPt,MD5是非常弱的,并且不适合于校验以外的任何东西。实际上,泰勒我需要根据用户类型使用两个数据库检查用户身份验证。在一个数据库中,我为用户类型的用户编码了密码。如果未找到该用户类型的输入用户,我需要切换到另一个数据库,在该数据库中,我将密码作为原始密码,因此我需要输入原始密码。您对此有何想法?您不知道需要原始密码。您只需使用与前端相同的方法对数据库中的密码进行散列,然后比较散列。如何使用此类在daoorg.springframework.security.authentication.encoding中获取原始密码。PlaintextPasswordEncoder@TaylorActually我需要用户在Dao中提供密码,就像我们使用HttpServletRequest来获取请求一样参数。是否可能?如果您使用上面的方法,它应该在auth令牌中。@holmisWe可以使用这种方法在用户经过身份验证后获取。对吗?@DIVA是的,在进行身份验证时。我需要用户凭据才能对用户进行身份验证。然后,只有我才能实现场景。是否可以在Dao中使用HttpServletRequest。我曾经使用过类似下面的方法,但我遇到了类似下面这样的错误:“未找到线程绑定请求:您是在实际web请求之外引用请求属性,还是在原始接收线程之外处理请求?”。您能帮我吗?@DIVA No,您不能还原散列密码。@homisys。我同意,但是有没有办法在Dao中获取请求参数。我可以在一个普通类中获取请求参数,如下面的代码。我想在Dao中使用它。