zoukankan      html  css  js  c++  java
  • SPRING IN ACTION 第4版笔记-第九章Securing web applications-008-使用非关系型数据库时如何验证用户(自定义UserService)

    一、

    1.定义接口

    Suppose that you need to authenticate against users in a non-relational database such
    as Mongo or Neo4j. In that case, you’ll need to implement a custom implementation
    of the UserDetailsService interface.

    1 public interface UserDetailsService {
    2     UserDetails loadUserByUsername(String username) throws UsernameNotFoundException;
    3 }

    2.实现接口

    All you need to do is implement the loadUserByUsername() method to find a user
    given the user’s username. loadUserByUsername() then returns a UserDetails object
    representing the given user. The following listing shows an implementation of
    UserDetailsService that looks up a user from a given implementation of Spitter-
    Repository

     1 package spittr.security;
     2 import org.springframework.security.core.GrantedAuthority;
     3 import org.springframework.security.core.authority.
     4 SimpleGrantedAuthority;
     5 import org.springframework.security.core.userdetails.User;
     6 import org.springframework.security.core.userdetails.UserDetails;
     7 import org.springframework.security.core.userdetails.
     8 UserDetailsService;
     9 import org.springframework.security.core.userdetails.
    10 UsernameNotFoundException;
    11 import spittr.Spitter;
    12 import spittr.data.SpitterRepository;
    13 
    14 public class SpitterUserService implements UserDetailsService {
    15 
    16     private final SpitterRepository spitterRepository;
    17 
    18     public SpitterUserService(SpitterRepository spitterRepository) {
    19         this.spitterRepository = spitterRepository;
    20     }
    21 
    22     @Override
    23     public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    24         Spitter spitter = spitterRepository.findByUsername(username);
    25         if (spitter != null) {
    26             List < GrantedAuthority > authorities = new ArrayList < GrantedAuthority > ();
    27             authorities.add(new SimpleGrantedAuthority("ROLE_SPITTER"));
    28             return new User(
    29                 spitter.getUsername(),
    30                 spitter.getPassword(),
    31                 authorities);
    32         }
    33         throw new UsernameNotFoundException("User '" + username + "' not found.");
    34     }
    35 }

    What’s interesting about SpitterUserService is that it has no idea how the user data
    is persisted. The SpitterRepository it’s given could look up the Spitter from a rela-
    tional database, from a document database, from a graph database, or it could just
    make it up. SpitterUserService doesn’t know or care what underlying data storage is
    used. It just fetches the Spitter object and uses it to create a User object. ( User is a
    concrete implementation of UserDetails .)

    3.配置service

    To use SpitterUserService to authenticate users, you can configure it in your
    security configuration with the userDetailsService() method:

    @Autowired
    SpitterRepository spitterRepository;
    @Override
    protected void configure(AuthenticationManagerBuilder auth)
    throws Exception {
        auth
            .userDetailsService(new SpitterUserService(spitterRepository));
    }

    The userDetailsService() method (like jdbcAuthentication() , ldapAuthentication ,
    and inMemoryAuthentication() ) configures a configuration store. But instead of using
    one of Spring’s provided user stores, it takes any implementation of UserDetailsService .
    Another option worth considering is that you could change Spitter so that it
    implements UserDetailsService . By doing that, you could return the Spitter
    directly from the loadUserByUsername() method without copying its values into a
    User object.

  • 相关阅读:
    2019年10月24日打印个人信息清单
    vsftp安装
    网络连接
    mysql多种方法修改密码----5.6的坑
    openstack-L版安装
    openstack是什么
    kvm快照
    文件操作
    函数
    virt-manage图形界面键盘错位问题
  • 原文地址:https://www.cnblogs.com/shamgod/p/5253011.html
Copyright © 2011-2022 走看看