zoukankan      html  css  js  c++  java
  • SPRING IN ACTION 第4版笔记-第九章Securing web applications-004-对密码加密passwordEncoder

    一、

    1.Focusing on the authentication query, you can see that user passwords are expected to be stored in the database. The only problem with that is that if the passwords are stored in plain text, they’re subject to the prying eyes of a hacker. But if you encode the password in the database, then authentication will fail because it won’t match the plain text password submitted by the user.

     1 @Override
     2 protected void configure(AuthenticationManagerBuilder auth)
     3 throws Exception {
     4     auth
     5         .jdbcAuthentication()
     6         .dataSource(dataSource)
     7         .usersByUsernameQuery(
     8             "select username, password, true " +
     9             "from Spitter where username=?")
    10         .authoritiesByUsernameQuery(
    11             "select username, 'ROLE_USER' from Spitter where username=?")
    12         .passwordEncoder(new StandardPasswordEncoder("53cr3t"));
    13 }

    passwordEncoder方法接收PasswordEncoder接口的实现为参数,Spring提供了有3种实现:BCryptPasswordEncoder , NoOpPasswordEncoder , andStandardPasswordEncoder

    接口代码如下:

    public interface PasswordEncoder {
        String encode(CharSequence rawPassword);
        boolean matches(CharSequence rawPassword, String encodedPassword);
    }

    it’s important to understand that the password in the database is never decoded. Instead, the password that the user enters at login is encoded using the same algorithm and is then compared with the encoded password in the database. That comparison is performed in the PasswordEncoder ’s matches() method.

  • 相关阅读:
    Http协议和Tomcat服务器
    类加载器与反射
    线程安全
    String、Stringbuffer、Stringbuilder三者之间的区别
    iOS 开发,工程中如何混合使用 ARC 和非ARC
    dll的静态调用、动态调用
    Qt安装—搭建VS2008+QT开发环境
    C++中的引用与指针的区别
    SVN分支与合并
    (补充知识)DLL 中 .DEF文件的使用
  • 原文地址:https://www.cnblogs.com/shamgod/p/5250069.html
Copyright © 2011-2022 走看看