問題は、私がthis tutorialという結果になっています。Spring Boot、Spring Security、MySQL - CustomUserDetailsServiceは常に「無効なユーザー名またはパスワード」というエラーを表示します
私の問題は、私はいつも私のユーザがデータベースから読み込まれているが、私は認証されていないという問題で動いているということです。ビューには、常に「無効なユーザー名またはパスワード」というエラーメッセージが表示されます。私のコンソール出力はエラーを表示しません。私の認証プロセスを通して私がデバッグしたとき、エラーがどこから来るのか不明確な挙動はなかった。
私のpom.xmlに私は以下の依存関係を使用しました。
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
My CustomUserDetailsService.javaはこのようになります。
@Service("customUserDetailsService")
public class CustomUserDetailsService implements UserDetailsService {
private final UserRepository userRepository;
private final UserRolesRepository userRolesRepository;
@Autowired
public CustomUserDetailsService(UserRepository userRepository,
UserRolesRepository userRolesRepository) {
this.userRepository = userRepository;
this.userRolesRepository = userRolesRepository;
}
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userRepository.findByUserName(username);
if (user == null) {
throw new UsernameNotFoundException("No user present with username " + username);
}
else {
List<String> userRoles = userRolesRepository.findRoleByUserName(username);
return new CustomUserDetails(user, userRoles);
}
}
}
CustomUserDetails.java
public class CustomUserDetails extends User implements UserDetails {
private static final long serialVersionUID = 1L;
private List<String> userRoles;
public CustomUserDetails(User user, List<String> userRoles) {
super(user);
this.userRoles = userRoles;
}
@Override
public Collection< ? extends GrantedAuthority> getAuthorities() {
String roles = StringUtils.collectionToCommaDelimitedString(userRoles);
return AuthorityUtils.commaSeparatedStringToAuthorityList(roles);
}
@Override
public boolean isAccountNonExpired() {
return false;
}
@Override
public boolean isAccountNonLocked() {
return false;
}
@Override
public boolean isCredentialsNonExpired() {
return false;
}
}
WebSecurityConfig.java
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordencoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/", "/home", "/images/**", "/css/**", "/js/**", "/loadEvents")
.permitAll().anyRequest().authenticated().and().formLogin().loginPage("/login")
.permitAll().and().logout().permitAll();
}
@Bean(name = "passwordEncoder")
public PasswordEncoder passwordencoder() {
return new BCryptPasswordEncoder();
}
}
私はエラーがデータベース上occuresないと思う前に私が述べたように。私はチュートリアルのデータベーススキームを使用しました。また、User.java UserRole.javaとチュートリアルの2つのリポジトリも使用しました。
My Application.javaは次のようになります。
@SpringBootApplication
@EnableJpaRepositories(basePackages = "<package>")
@EntityScan(basePackages = "<package>")
public class Application {
public static void main(String[] args) throws Throwable {
SpringApplication.run(Application.class, args);
}
}
アップデート1:Gitのプロジェクト https://github.com/pStuetz/Speeddating4SO/tree/master
へのリンクは、あなたはおそらく、あなたのデータベースをサポートするためのsrc /メイン/リソース/ application.propertiesを編集する必要があります。私は自分のデータベーステーブルを作成するために使用したSQLスクリプトを含んでいました。
ソースコードを共有していただけますか?指定されたチュートリアルのアプリケーションが正しく動作していない、ページがなくなったなどの不満があります。 –
返信いただきありがとうございます。私はgitプロジェクトへのリンクを追加しました。 – sttzP