problems injecting custom userDetailsService in Spring Security OAuth2

前端 未结 3 865
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-22 05:33

I am using Spring Security OAuth2 2.0.7.RELEASE. As i am using ORM to connect to my database and default JdbcUserDetailsManager uses jdbc i wanted to implement my own UserDetail

3条回答
  •  借酒劲吻你
    2021-01-22 06:00

    The problem is that you are using default JdbcDaoImpl. The query which is causing the problem is

    public static final String DEF_AUTHORITIES_BY_USERNAME_QUERY =
            "select username,authority " +
            "from authorities " +
            "where username = ?";
    

    This is used as a default query to load all authorities for the user by userName inside JdbcDaoImpl. So if you still want to use default JdbcDaoImpl - you can set the custom query, which will do the job with your tables, as the parameter of it.

    I'm not sure what is the schema of you users table, but something like this should be similar(if you are configuring JdbcDaoImpl bean programmatically):

    String query = "select username,authority " +
            "from authorities join users on users.id = authorities.user_id " +
            "where users.username = ?";
    jdbcDaoImpl.setAuthoritiesByUsernameQuery(query);
    

    Or if you create JdbcDaoImpl from XML:

    
        
    
    

    There might be some other default queries that you would like to change to fit your schema, take a look at them inside JdbcDaoImpl.

    You might also consider a possibility of writing your own implementation of UserDetailsService if it will start getting too far from default JdbcDaoImpl one.

提交回复
热议问题