Spring Framework没有找到具有正确查询权限的权限

时间:2016-02-10 17:18:36

标签: java mysql spring spring-security

我们遇到Spring问题。 我们的身份验证功能在数据库中找不到用于指定用户角色的记录。 您可以在下面找到我们的权限查询,用户存在,但身份验证是通过电子邮件(用户名)完成的,角色表只包含用户Account_ID。

手动查询数据库会返回1个确实正确的数据集,但是春天告诉我们以下内容:

DEBUG JdbcTemplate - Executing prepared SQL query
DEBUG JdbcTemplate - Executing prepared SQL statement [Select Account_ID,password,activated from Account where email=?]
DEBUG DataSourceUtils - Fetching JDBC Connection from DataSource
DEBUG DriverManagerDataSource - Creating new JDBC DriverManager Connection to [jdbc:mysql://*****]
DEBUG DataSourceUtils - Returning JDBC Connection to DataSource
DEBUG JdbcTemplate - Executing prepared SQL query
DEBUG JdbcTemplate - Executing prepared SQL statement [SELECT Account_ID,role FROM Account_Roles WHERE Account_ID=(SELECT Account_ID from Account WHERE email=?)]
DEBUG DataSourceUtils - Fetching JDBC Connection from DataSource
DEBUG DriverManagerDataSource - Creating new JDBC DriverManager Connection to [jdbc:mysql://****]
DEBUG DataSourceUtils - Returning JDBC Connection to DataSource
DEBUG JdbcUserDetailsManager - User 'admin@admin.de' has no authorities and will be treated as 'not found'
DEBUG DaoAuthenticationProvider - User 'admin@admin.de' not found

我们的权威查询:

SELECT Account_ID,role FROM Account_Roles WHERE Account_ID=(SELECT Account_ID from Account WHERE email=?)

我们的身份验证方法:

@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception
{
    String usersByEmailQuery = "Select Account_ID,password,activated from Account where email=?";
    String authoritiesByUsernameQuery = "Select Account.email,Account_Roles.role from Account_Roles join Account on Account_Roles.Account_ID=Account.Account_ID where Account.email=?";
    String authoritiesByUsernameQueryNew = "SELECT Account_ID,role FROM Account_Roles WHERE Account_ID=(SELECT Account_ID from Account WHERE email=?)";

    auth.jdbcAuthentication().dataSource(dataSource)
        .usersByUsernameQuery(
            usersByEmailQuery)
        .authoritiesByUsernameQuery(
            authoritiesByUsernameQueryNew)
        .passwordEncoder(bCryptPasswordEncoder);
}

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

问题已解决

问题是,在第一次查询之后,主键在第二个查询中使用,而不是在第一个查询中使用的相同参数,因此第二个查询最终搜索了email = 1(Account_ID) 。要指定它:

CandidateListCompletionHandler

所以查询现在看起来像这样:

  @Override
   public boolean complete(final ConsoleReader reader, final List<CharSequence> candidates, final int pos)
         throws IOException {
      CursorBuffer buf = reader.getCursorBuffer();

      // THIS IS NEW
      String[] allWords = buf.toString().split(" ");
      String firstWords = "";
      if (allWords.length > 1) {
         for (int i = 0; i < allWords.length - 1; i++) {
            firstWords += allWords[i] + " ";
         }
      }
      //-----

      // if there is only one completion, then fill in the buffer
      if (candidates.size() == 1) {
         String value = Ansi.stripAnsi(candidates.get(0).toString());

         if (buf.cursor == buf.buffer.length() && this.printSpaceAfterFullCompletion && !value.endsWith(" ")) {
            value += " ";
         }

         // fail if the only candidate is the same as the current buffer
         if (value.equals(buf.toString())) {
            return false;
         }

         CandidateListCompletionHandler.setBuffer(reader, firstWords + " " + value, pos);

         return true;
      } else if (candidates.size() > 1) {
         String value = this.getUnambiguousCompletions(candidates);
         CandidateListCompletionHandler.setBuffer(reader, value, pos);
      }

      CandidateListCompletionHandler.printCandidates(reader, candidates);

      // redraw the current console buffer
      reader.drawLine();

      return true;
   }