Spring-Security您的登录尝试失败,请重试

时间:2018-09-11 06:27:18

标签: java html spring authentication spring-security

我正在从事Spring Boot项目。但是我对弹簧安全性有疑问。 我可以注册,但无法登录。 当我尝试登录时,仅给出此错误:和?url错误: 您的登录尝试失败,请重试。 原因:凭证不正确 并且soesnt从控制台给出任何错误。 我检查了成功注册的用户。

安全配置:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{

    @Autowired
    private DataSource dataSource;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication().dataSource(dataSource)
        .usersByUsernameQuery("select email as principal, password as crendentials, true from user where email=?")
        .authoritiesByUsernameQuery("select user_id as principal, role_name as role from user_roles where user_id=?")
        .passwordEncoder(passwordEncoder()).rolePrefix("ROLE_");
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        // TODO Auto-generated method stub
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        http.authorizeRequests().antMatchers("/register", "/login", "/", "/welcome", "/static/**").permitAll()
        .anyRequest().authenticated().and().formLogin()
        .loginPage("/login")
        .permitAll().defaultSuccessUrl("/profile").and().logout()
        .logoutSuccessUrl("/login");
    }
}

控制器:

@RequestMapping("/login")
    public String login(HttpServletRequest request) {
        request.setAttribute("mode", "MODE_LOGIN");
        return "welcomepage";
    }

角色数据库:

@Entity
public class Role {

    @Id
        private String name;
        @ManyToMany(mappedBy="roles")
        private List<User> users;

用户数据库:

@Entity
public class User {

    @Id
        @Column(unique = true)
        @NotNull
        @GeneratedValue
        private Long id;
        @NotEmpty
        private String username;
        @Email
        @Column(unique = true)
        @NotEmpty
        private String email;
        @NotEmpty
        private String firstname;
        @NotEmpty
        private String lastname;
        @NotNull
        private int age;
        @Size(min = 4)
        private String password;
        @ManyToMany(cascade = CascadeType.ALL)
        @JoinTable(name = "USER_ROLES", joinColumns = {
                @JoinColumn(name = "USER_ID", referencedColumnName = "id") }, inverseJoinColumns = {
                        @JoinColumn(name = "ROLE_NAME", referencedColumnName = "name") })
        private List<Role> roles;

有我的登录页面html:

 <c:when test="${mode=='MODE_LOGIN' }">
                    <div class="container text-center">
                        <h3>User Login</h3>
                        <hr>
                        <form class="form-horizontal" method="POST" action="/login">
                            <c:if test="${not empty param}">
                                <div class="alert alert-danger">
                                    <h2>Invalid Email or Password</h2>
                                </div>
                            </c:if>
                            <div class="form-group">
                                <label class="control-label col-md-3">Email</label>
                                <div class="col-md-7">
                                    <input type="text" class="form-control" name="username"
                                        value="${user.email }" id="email" />
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label col-md-3">Password</label>
                                <div class="col-md-7">
                                    <input type="password" class="form-control" name="password"
                                        value="${user.password }" id="password" />
                                </div>
                            </div>
                            <div class="form-group ">
                                <input type="submit" class="btn btn-primary" value="Login" />
                            </div>
                        </form>
                    </div>
                </c:when>
            </c:choose>

0 个答案:

没有答案