Spring MVC Spring安全性SQL异常

bvhaajcl  于 2023-01-13  发布在  Spring
关注(0)|答案(1)|浏览(109)

我已经将Spring Security附加到我的学习项目中。我决定遵循这个教程:但是当我尝试登录时,我得到java.sql.SQLException:参数索引超出范围(1〉参数数,即0)。
服务器日志:
我花了很多时间想找出毛病所在,但还是不明白为什么它不工作。
我的安全配置:

@Configuration
@EnableWebSecurity
public class MySecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;

@Autowired
private DataSource dataSource;
@Value("${spring.queries.users-query}")
private String usersQuery;

@Value("${spring.queries.roles-query}")
private String rolesQuery;

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.jdbcAuthentication().usersByUsernameQuery(usersQuery).authoritiesByUsernameQuery(rolesQuery)
            .dataSource(dataSource).passwordEncoder(bCryptPasswordEncoder);
}

@Override
protected void configure(HttpSecurity http) throws Exception {

    http.authorizeRequests()
            // URLs matching for access rights
            .antMatchers("/").permitAll()
            .antMatchers("/login").permitAll()
            .antMatchers("/register").permitAll()
            .antMatchers("/home/**").hasAnyAuthority("SUPER_USER", "ADMIN_USER", "SITE_USER")
            .anyRequest().authenticated()

            .and()

            // form login
            .csrf().disable().formLogin()
            .loginPage("/login")
            .failureUrl("/?error=true")
            .defaultSuccessUrl("/home")
            .usernameParameter("email")
            .passwordParameter("password")
            .and()
            // logout
            .logout()
            .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
            .logoutSuccessUrl("/").and()
            .exceptionHandling()
            .accessDeniedPage("/access-denied");
}

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/resources/**", "/static/**", "/css/**", "/js/**", "/images/**");
   }

}

我的控制器:

@Controller
public class AuthenticationController {

@RequestMapping(value = { "/login" }, method = RequestMethod.GET)
public ModelAndView login() {

    ModelAndView modelAndView = new ModelAndView();

    modelAndView.setViewName("login"); 
    return modelAndView;
}
@RequestMapping(value = "/register", method = RequestMethod.GET)
public ModelAndView register() {
    ModelAndView modelAndView = new ModelAndView();
    // User user = new User();
    // modelAndView.addObject("user", user);
    modelAndView.setViewName("register"); 
    return modelAndView;
}

@RequestMapping(value = "/home")
public ModelAndView home() {
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("home"); 
    return modelAndView;
}
}

和jsp:

<!DOCTYPE html>
   <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
   <head>
   <title>English - login</title>
   </head>
   <body>
      <form th:action="@{/login}" method="POST">
      Login
      <br>
      <input type="text" id="email" name="email" th:placeholder="Email"/>
      <br>
      Password
      <br>
      <input type="password" id="password" name="password" th:placeholder="Password"/>
      <br>
      <br>
      <button name="Submit" value="Login" type="Submit" th:text="Login"></button>
      <a href="/recover-password">Forgot password?</a>
      </form>
   </body>
   </html>

我的属性文件:

#Spring Security login queries
 security.basic.enabled=false
 spring.queries.users-query=select email, password, '1' as enabled from auth_user where email=? and  status='VERIFIED'
 spring.queries.roles-query=select u.email, r.role_name from auth_user u inner join auth_user_role ur on(u.auth_user_id=ur.auth_user_id) inner join auth_role r on(ur.auth_role_id=r.auth_role_id) where u.email=?

正如我所看到的,参数从我的页面转到控制器:

这个问题,正如我对sql语句的理解。但是对我来说,我的sql语句就像JdbcDaoImpl.class中的默认语句一样。我根据这个例外找了所有的解决方案,但没有找到答案。请帮助。我希望我已经附上了所有必要的信息。如果有任何代码也应该附上这里-请写信。
我试着用其他方法重建我的表(2个表-用户和权限),试着在sql语句中使用'?'和?,试着将搜索从email改为username,但都不起作用(
链接到项目GITHUB https://github.com/SerhiiRyzhkov/english

lbsnaicq

lbsnaicq1#

我认为在您的配置文件中,您将usersQuery作为带双引号的字符串值传递,而不是传递以字符串格式保存实际查询的变量。因为在查询中,您有一个占位符“?”表示不在“usersQuery”字符串中的参数,它甚至不是一个实际查询。
尝试将配置更改为:
jdbc认证().用户按用户名查询(用户查询).授权按用户名查询(角色查询).数据源(数据源).密码编码器(b加密密码编码器);

相关问题