@ComponentScan(basePackages = {"conf"})
@ComponentScan(basePackages = {"application.controller"})
@ComponentScan(basePackages = {"applicaion.model"})
@ComponentScan(basePackages = {"applicaion.dao"})
@ComponentScan(basePackages = {"usersDetails"})
@SpringBootApplication
@EnableJpaRepositories
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
安全配置部分
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Bean
@Override
public UserDetailsService userDetailsService() {
return super.userDetailsService();
}
@Override
@Autowired
public void configure(AuthenticationManagerBuilder auth) throws Exception{
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.formLogin()
.permitAll();
}
@Bean
public PasswordEncoder passwordEncoder() {return NoOpPasswordEncoder.getInstance();}
}
用戶物體
“felhasznalonev”==用戶名和“felhasznalo”==資料庫表中匈牙利語中的用戶具有這些名稱
@Entity
@Table( name="felhasznalo")
public class User {
@Id
@GeneratedValue
private int id;
@Column( unique=true, nullable=false )
private String felhasznalonev;
@Column( nullable=false )
private String jelszo;
private int statusz;
public User() {}
public User(String felhasznalonev,String jelszo,int statusz) {
this.felhasznalonev=felhasznalonev;
this.jelszo=jelszo;
this.statusz=statusz;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFelhasznalonev() {
return felhasznalonev;
}
public void setFelhasznalonev(String email) {
this.felhasznalonev = email;
}
public String getJelszo() {
return this.jelszo;
}
public void setPassword(String password) {
this.jelszo = password;
}
@Override
public String toString() {
return null;
}
public int getStatusz() {
return statusz;
}
public void setStatusz(int statusz) {
this.statusz = statusz;
}
}
用戶服務實作部分
@Service("userDetailsService")
public class UserServiceImpl implements UserService, UserDetailsService {
@Autowired
private UserRepository userRepository;
@Autowired
public UserServiceImpl(UserRepository userRepository){
this.userRepository = userRepository;
}
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = findByUsername(username);
return new UserDetailsImpl(user);
}
@Override
public User findByUsername(String username) {
return userRepository.findByUsername(username);
}
}
UserDetailsImpl 部分
public class UserDetailsImpl implements UserDetails {
private User user;
public UserDetailsImpl(User user) {
this.user = user;
}
public UserDetailsImpl() {}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return Arrays.asList(new SimpleGrantedAuthority("USER"));
}
@Override
public String getPassword() {
return user.getJelszo();
}
@Override
public String getUsername() {
return user.getFelhasznalonev();
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}
用戶服務部分
public interface UserService {
public User findByUsername(String username);
}
用戶庫
public interface UserRepository extends JpaRepository<User,Integer> {
User findByUsername(String username);
}
當我運行代碼時,一切看起來都很好,進入了基本的登錄頁面,我從資料庫中輸入了用戶名/密碼,但沒有任何反應
和 IntellIj 寫這個:
2021-11-25 13:12:48.870 錯誤 13928 --- [nio-8080-exec-5] oaccC[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] 在背景關系中路徑 [] 拋出例外 [過濾器執行拋出例外] 與根本原因
java.lang.StackOverflowError: 在 org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsS??erviceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:472) 處為空~[spring-security-config-5.3.4.RELEASE.jar: 5.3.4.RELEASE] 在 org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsS??erviceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:472) ~[spring-security-config-5.3.4.RELEASE.jar:5.3 .4.發布] -||-
與資料庫的連接很好,我也可以列出用戶 感謝您閱讀所有這些,對于糟糕的英語和錯誤表示抱歉,祝您有美好的一天!
uj5u.com熱心網友回復:
java.lang.StackOverflowError錯誤告訴您service層中的方法宣告未與任何JpaRepository. 問題是從上來loadUserByUsername的方法userServiceimpl。您宣告方法findByUsername而不與Repository.
改變
User user = findByUsername(username);
到
User user = userRepository.findByUsername(username);
并且只UserServiceImpl執行UserDetailsService。您需要更改Security config代碼,因為它有更多問題,例如添加錯誤的注釋和兩個具有相同名稱的方法宣告等...
修改的 Security config
@EnableWebSecurity
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter{
@Autowired
private UserDetailsService userDetailsService;
@Bean
public AuthenticationProvider authProvider() {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setUserDetailsService(userDetailsService);
provider.setPasswordEncoder(passwordEncoder());
return provider;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.formLogin()
.permitAll();
}
@Bean
public PasswordEncoder passwordEncoder()
{
return new BCryptPasswordEncoder();
}
}
uj5u.com熱心網友回復:
您已經雙重宣告了同名的 userDetailsS??ervice,
第一的:
@Bean
@Override
public UserDetailsService userDetailsService() {
return super.userDetailsService();
}
第二:
@Service("userDetailsService")
public class UserServiceImpl implements UserService, UserDetailsService {
它可能會導致問題。您應該只有一個 userDetailService 實體。
uj5u.com熱心網友回復:
在您的 SecurityConfig 中,您可以嘗試洗掉
@Autowired
private UserDetailsService userDetailsService;
@Bean
@Override
public UserDetailsService userDetailsService() {
return super.userDetailsService();
}
并改變實作
@Override
@Autowired
public void configure(AuthenticationManagerBuilder auth) throws Exception{
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
到
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception{
auth.userDetailsService(new UserServiceImpl()).passwordEncoder(passwordEncoder());
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/366568.html
標籤:春天 PostgreSQL的 弹簧靴 弹簧安全
