Iチュートリアルhttp://www.concretepage.com/spring-boot/spring-boot-security-rest-jpa-hibernate-mysql-crud-exampleなぜ私はSpringブートセキュリティの基本認証で403を取得していますか?
注従った:SYSOUTはDBから正しい値を出力
@Override
public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
System.out.println("UserName: " + userName);
User activeUserInfo = userRepository.findByUserName(userName);
System.out.println("Role D : " + activeUserInfo.getUserRole().getName()); //This prints 'ADMIN' as expected
GrantedAuthority authority = new SimpleGrantedAuthority(activeUserInfo.getUserRole().getName());
System.out.println("User : " + activeUserInfo.getUserName()); //This prints my username 'test123' as expected
System.out.println("PWD : " + activeUserInfo.getPassword()); //This prints '$2a$10$PN/MwjCHtCWsI4auW6Q8AOxb4dug4WSO8tfINySqHZ8eYVjFKIAW6' as expected
System.out.println("Authority : " + authority);
UserDetails userDetails = (UserDetails) new org.springframework.security.core.userdetails.User(activeUserInfo.getUserName(), activeUserInfo.getPassword(),
Arrays.asList(authority));
return userDetails;
}
UserDetailsService.java
以下の私のコードである
Working on Spring-boot, Spring Data JPA, Security and basic authentication
を。
SecurityConfiguration.java
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().authorizeRequests().antMatchers("/server/rest/secure/**").hasAnyRole("ADMIN", "Non Admin").and()
.httpBasic().realmName(REALM).authenticationEntryPoint(new CustomBasicAuthenticationEntryPoint());
}
CustomBasicAuthenticationEntryPointは
public class CustomBasicAuthenticationEntryPoint extends BasicAuthenticationEntryPoint {
@Override
public void commence(final HttpServletRequest request,
final HttpServletResponse response,
final AuthenticationException authException) throws IOException, ServletException {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.addHeader("WWW-Authenticate", "Basic realm=" + getRealmName() + "");
PrintWriter writer = response.getWriter();
writer.println("HTTP Status 401 : " + authException.getMessage());
response.setHeader("WWW-Authenticate", "FormBased");
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized");
}
@Override
public void afterPropertiesSet() throws Exception {
setRealmName("MY_TEST_REALM");
super.afterPropertiesSet();
}
}
を.javaファイルしかし、ログイン時に、私はstatus":403,"error":"Forbidden","message":"Access is denied"
注:私はBase64を暗号化/復号化パスワードに使用しました。しかし、この例に基づいてBCryptに更新されました。だから今私はBCryptのパスワードをオンラインで生成したテスト目的のために。私のpwdは "テスト中"です。BCryptのpwdは "$ 2a $ 10 $ PN/MwjCHtCWsI4auW6Q8AOxb4dug4WSO8tfINySqHZ8eYVjFKIAW6"です。私はこれを間違えましたか? 私のコードで何が問題になっていますか?
私はあなたがSecurityConfiguration.javaに私のコードを試してみてください.permit()
最終的にはいけないと思い
です()); 'あなたは' passwordEncoder'をBeanとして宣言する必要があります。 –