2017-09-20 7 views
-1

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; 
} 

UserDetailsS​​ervice.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()最終的にはいけないと思い

答えて

0

protected void configure(HttpSecurity http) throws Exception { 
     http.csrf().disable().cors().disable().httpBasic().and().authorizeRequests() 
     .antMatchers(PUBLIC_MATCHERS).permitAll().anyRequest().authenticated(); 
    } 

@Autowired 
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
    auth.userDetailsService(userSecurityService).passwordEncoder(passwordEncoder()); 
} 

PS:PUBLIC_MATCHERSは `auth.userDetailsS​​ervice(userSecurityService).passwordEncoder(passwordEncoderを使用する前に

private static final String[] PUBLIC_MATCHERS = { 
with your folders 
    }; 
+0

です()); 'あなたは' passwordEncoder'をBeanとして宣言する必要があります。 –

関連する問題