简体   繁体   English

Spring Boot Security 4自定义PasswordEncoder

[英]Spring Boot Security 4 custom PasswordEncoder

I have a password field in the database containing MD5(username + password). 我在包含MD5(用户名+密码)的数据库中有一个密码字段。 How would I implement this in Spring Security + JdbcAuthentication. 我将如何在Spring Security + JdbcAuthentication中实现这一点。 I know that this is not secure by any means, but it is a legacy database, which I must talk to. 我知道这绝对不安全,但是它是旧数据库,我必须与之讨论。 My current code looks like this: 我当前的代码如下所示:

auth.jdbcAuthentication()
    .dataSource(dataSource)
    .usersByUsernameQuery("select login, password, 1 as enabled from 
                           login where login=?")
    .authoritiesByUsernameQuery("select login,role" +
                    "  from login lo" +
                    "  join login_role lor on lo.login_id = lor.login_id" +
                    "  join role gr on lor.role_id = gr.role_id" +
                    " where login=?")

If I send the hashed value as password the authentication works. 如果我将哈希值作为密码发送,则身份验证有效。 I think I would have to configure some password encoder. 我想我必须配置一些密码编码器。

You can configure the whole authentication into your applicationContext.xlm like this: 您可以像这样将整个身份验证配置到applicationContext.xlm中:

    <sec:authentication-manager>
        <sec:authentication-provider>
            <sec:password-encoder ref="encoder" /> // And here is an encoder that will encode a password which comes from login form.
            <sec:jdbc-user-service data-source-ref="dataSource"
                users-by-username-query="your query"
                authorities-by-username-query="your query for authentication table" />
        </sec:authentication-provider>
    </sec:authentication-manager>

I tried it in school today and it worked for me. 我今天在学校尝试过,对我有用。 I found it easiest way to solve my authority problem and hope that it also will be suitable solution for you. 我发现这是解决我的权限问题的最简单方法,希望它也将是适合您的解决方案。

PS. PS。 Sorry, not sure about how correct my answer will be displayed. 抱歉,不确定我的答案将如何显示。 Was only reader here for about an year and now decided to join and write some questions myself 仅在这里读过一年的读者,现在决定自己加入并写一些问题

I've gone with the hacky way and rewrote the Request Parameters in a ServletFilter. 我采用了骇人听闻的方法,并在ServletFilter中重写了Request Parameters。 The Key for this was to use a custom HttpServletRequestWrapper to be able to modify the request. 这样做的关键是使用自定义HttpServletRequestWrapper能够修改请求。

Now username+password is passed as password to the md5 encoder. 现在,用户名和密码作为密码传递给md5编码器。 Had some upper/lowercase Problems, but adjusting SQL solved this. 遇到一些大写/小写问题,但是调整SQL可以解决此问题。

You should do like following 您应该喜欢以下内容

@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) 
    throws Exception {

    auth.jdbcAuthentication().dataSource(dataSource)
        .passwordEncoder(passwordEncoder())
        .usersByUsernameQuery("sql...")
        .authoritiesByUsernameQuery("sql...");
}   

@Bean
public PasswordEncoder passwordEncoder(){
    PasswordEncoder encoder = new BCryptPasswordEncoder();
    return encoder;
}

Hope its help to you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM