How to add a client using JDBC for ClientDetailsServiceConfigurer in Spring?

后端 未结 5 1536
迷失自我
迷失自我 2021-01-30 09:58

I have the in memory thing working as follows:

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

        clients.inMemo         


        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-30 10:23

    @AndroidLover 's answer is good, but it could be simplified. You don't need to create tables like oauth_access_token, oauth_refresh_token, etc. unless you need a jdbc token store.

    Since you only need a jdbc client detail service, all you need to do is:
    1. create a client detail table oauth_client_details, for example:

    drop table if exists oauth_client_details;
        create table oauth_client_details (
        client_id VARCHAR(255) PRIMARY KEY,
        resource_ids VARCHAR(255),
        client_secret VARCHAR(255),
        scope VARCHAR(255),
        authorized_grant_types VARCHAR(255),
        web_server_redirect_uri VARCHAR(255),
        authorities VARCHAR(255),
        access_token_validity INTEGER,
        refresh_token_validity INTEGER,
        additional_information VARCHAR(4096),
        autoapprove VARCHAR(255)
        );
    

    2. create a user model that implements the UserDetail interface, for example(I'm using spring jpa in this case, you could use mybatis, jdbc, whatever):

    @Entity
    @Table(name = "users")
    public class User implements UserDetails {
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "user_id", nullable = false, updatable = false)
    private String id;
    
    @Column(name = "username", nullable = false, unique = true)
    private String username;
    
    @Column(name = "password", nullable = false)
    private String password;
    
    @Column(name = "enabled", nullable = false)
    @Type(type = "org.hibernate.type.NumericBooleanType")
    private boolean enabled;
    
    public String getId() {
        return id;
    }
    
    public void setId(String id) {
        this.id = id;
    }
    
    public void setUsername(String username) {
        this.username = username;
    }
    
    public void setPassword(String password) {
        this.password = password;
    }
    
    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }
    
    @Override
    public Collection getAuthorities() {
        List authorities = new ArrayList();
        authorities.add((GrantedAuthority) () -> "ROLE_USER");
        return authorities;
    }
    
    @Override
    public String getPassword() {
        return this.password;
    }
    
    @Override
    public String getUsername() {
        return this.username;
    }
    
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }
    
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }
    
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }
    
    @Override
        public boolean isEnabled() {
            return this.enabled;
        }
    }
    

    3. create a custom user detail service. notice that in your implementation, you should inject your dao service(in my case, I injected a jpaRepository.) and your dao service MUST have a method to find user by username.:

    @Service("userDetailsService")
    public class UserService implements UserDetailsService {
    
    @Autowired
    UserRepository userRepository;
    
    @Override
    public UserDetails loadUserByUsername(String userName) throws 
    UsernameNotFoundException {
        return userRepository.findByUsername(userName);
    }
    }
    

    4. finally, config you authentication server:

    @Configuration
    @EnableAuthorizationServer
    public class AuthServerConfig extends AuthorizationServerConfigurerAdapter {
    
    @Autowired
    @Qualifier("dataSource")
    DataSource dataSource;
    
    @Autowired
    @Qualifier("userDetailsService")
    private UserDetailsService userDetailsService;
    
    
    @Autowired
    private AuthenticationManager authenticationManager;
    
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer configurer) {
        configurer
                .authenticationManager(authenticationManager)                
                .approvalStoreDisabled()
                .userDetailsService(userDetailsService);
    }
    
    
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception 
    {
        clients
                .jdbc(dataSource)
                .inMemory().withClient("my-trusted-
    client").secret("secret").accessTokenValiditySeconds(3600)
                .scopes("read", "write").authorizedGrantTypes("password", 
    "refresh_token").resourceIds("resource");
    }
    }
    

提交回复
热议问题