它向我显示了默认登录表单和我的登录表单,我使用默认用户名和密码登录时间。在第一次向我播种我的登录表单时,当我尝试使用相同的默认用户名和密码登录时,它总是返回到第二个登录表单,代码为 stat:302。(我将员工保存在数据库中,但它总是让我出错)
@EnableWebSecurity
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
EmployeeDao employeeDao;
@Autowired
private DataSource dataSource;
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource).usersByUsernameQuery("select username,password
from employees where username=?")
.authoritiesByUsernameQuery("select username, authority from employees where username=?")
.passwordEncoder(bCryptPasswordEncoder);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests().antMatchers("/admin/**").hasRole("ADMIN").anyRequest().authenticated()
.and()
.formLogin().loginPage("/login").failureUrl("/login?error=true").defaultSuccessUrl("/home", true)
.and()
.logout().logoutSuccessUrl("/login?logout=true") ;
}
}
我的控制器:
@GetMapping({"/","/login"})
public String LoginForm() { return "login";}
@GetMapping("/home")
public String showhome(Model model) { return "index";}
我的实体:
@Entity
@Getter
@Setter
@Table(name = "employees")
public class Employee {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Column(name = "username", nullable = false)
private String username;
@Column(name = "password", nullable = false)
private String password;
@Column(name = "fullname", nullable = false)
private String fullname;
@Column(name = "authority", nullable = false)
private String authority;
@OneToMany(mappedBy = "employee", fetch = FetchType.EAGER)
@Cascade(CascadeType.ALL)
private List<Role> roles;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
public String getAuthority() {
return authority;
}
public void setAuthority(String authority) {
this.authority = authority;
}
public List<Role> getRoles() {
return roles;
}
public void setRoles(List<Role> roles) {
this.roles = roles;
}
}
我的登录表格:
<div class="divLogin" align="center">
<h1 clase="text-center">Login Page</h1>
<form class="addForm" th:action="@{/login}" method="post">
<fieldset>
<legend lase="text-center">Please Login</legend>
<input class="inputForm" type="text" id="username" name="username" placeholder="Username"/>
<input class="inputForm" type="password" id="password" name="password" placeholder="Passowrd"/>
<div class="divBottonLogin" align="center">
<button class="botten" type="submit">Log in</button>
</div>
</fieldset>
</form>
</div>