Spring security 错误的凭据Spring安全应用程序
当我使用SpringSecurity和acces时,我是SpringSecurity的新手 我的sql数据库中的用户名和密码,按signin时 按钮我得到了错误的登录凭证 我不知道发生了什么错误 我试图访问数据库中的数据 ApplicationConfig.java MyUserDetailsService.java 用户定位 LoginController.javaSpring security 错误的凭据Spring安全应用程序,spring-security,Spring Security,当我使用SpringSecurity和acces时,我是SpringSecurity的新手 我的sql数据库中的用户名和密码,按signin时 按钮我得到了错误的登录凭证 我不知道发生了什么错误 我试图访问数据库中的数据 ApplicationConfig.java MyUserDetailsService.java 用户定位 LoginController.java package com.example.CrudDemo.configuration; import java.util.Ar
package com.example.CrudDemo.configuration;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
@Configurable
@EnableWebSecurity
public class AppSecuringConfig extends WebSecurityConfigurerAdapter{
@Autowired
private UserDetailsService userDetailsService;
@Bean
public AuthenticationProvider authProvider(){
DaoAuthenticationProvider provider= new DaoAuthenticationProvider();
provider.setUserDetailsService(userDetailsService);
provider.setPasswordEncoder(NoOpPasswordEncoder.getInstance());
return provider;
}}
package com.example.CrudDemo.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import com.example.CrudDemo.Entity.User;
import com.example.CrudDemo.princple.UserPrincple;
import com.example.CrudDemo.repository.UserRepository;
@Service
public class MyUserDetailsService implements UserDetailsService {
@Autowired
private UserRepository userRepo;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
// TODO Auto-generated method stub
User user = userRepo.findByUsername(username);
if (user== null)
throw new UsernameNotFoundException("You are not a User");
return new UserPrincple(user);
}
}
package com.example.CrudDemo.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.example.CrudDemo.Entity.User;
public interface UserRepository extends JpaRepository<User, Long> {
User findByUsername(String username);
}
package com.example.CrudDemo.Entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotBlank;
import lombok.Data;
@Entity
@Table(name="stuuser")
@Data
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@Column(name="username",unique=true)
@NotBlank(message="UserName mandatory")
private String username;
@Column(name="password")
@NotBlank(message="pass word mandatory")
private String password;
public User(){}
}
package com.example.CrudDemo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class LoginContreoller {
@RequestMapping("/")
public String list(){
return "index.html";
}
}