0

私はSpring Securityに問題があります。spring security:mysql認証を作成する

私はmysqlデータチェックで認証をしようとしています。 私はAngularJs、Spring Boot、Spring Securityを使用しています。

私は$ http.post(...)によって呼び出されるwebservice restを持っています。 私は私のアプリを起動し、クロムプラグイン "Advanced Rest Client"> "http://localhost:8080/check-login"でウェブサービスをテストすると、それは動作し、私はコード200を受け取ります:OK。

ただし、同じURLのChrome経由でウェブサービスにアクセスしたい場合は、認証が開始されたウィンドウ。私はSpring Securityによる事前認証だと思います。しかし、私はそれを無効にする方法を知らない。

私は私のブラウザでWebサービスにアクセスするとき、それは言ったので、それが問題だ: "unauthaurized http://localhost:8080/check-login 401"

編集:これは私のコードです:

HTML:

<form role="form" ng-submit="controller.login()"> 

    <div class="form-group"> 
     <label for="username">Username:</label> 
     <input type="text" class="form-control" id="username" name="username" ng-model="controller.credentials.username"/> 
    </div> 

    <div class="form-group"> 
     <label for="password">Password:</label> 
     <input type="password" class="form-control" id="password" name="password" ng-model="controller.credentials.password"/> 
    </div> 

    <button type="submit" class="btn btn-primary">Submit</button> 

</form> 

JS:

myModule.controller('NavCtrl',function($rootScope, $location, $http){ 

    var self = this 

    var authenticate = function(credentials, callback) { 


     var headers = credentials ? {authorization : "Basic " + btoa(credentials.username + ":" + credentials.password)} : {}; 

     $http.get('user', {headers : headers}).then(
      function(response) { 
       if (response.data.name) { 
        $rootScope.authenticated = true; 
       } else { 
        $rootScope.authenticated = false; 
       } 
       callback && callback(); 
      }, 
      function() { 
       $rootScope.authenticated = false; 
       callback && callback(); 
      } 
     ); 

    } 

    authenticate(); 
    self.credentials = {}; 

    self.login = function() { 
     authenticate(self.credentials, function() { 
      if ($rootScope.authenticated) { 
       $location.path("/"); 
       self.error = false; 
      } else { 
       $location.path("/login"); 
       self.error = true; 
      } 
     }); 
    }; 

}); 

私のJavaのREST:

@RestController 
public class TestRest { 

    @RequestMapping("/user") 
    public Principal user(Principal user){ 
     return user; 
    } 

} 

私のJavaの春のブートとセキュリティの設定:

@SpringBootApplication 
public class BusinessBootApplication { 

    public static void main(String[] args) { 
     SpringApplication.run(BusinessBootApplication.class, args); 
    } 

    @Configuration 
    @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) 
    protected static class SecurityConfiguration extends WebSecurityConfigurerAdapter{ 
     @Override 
     protected void configure(HttpSecurity http) throws Exception { 
      http 
      .httpBasic() 
      .and() 
      .authorizeRequests() 
      .antMatchers("/index.html","/home.html","/login.html","/").permitAll() 
      .anyRequest() 
      .authenticated() 
      .and() 
      .addFilterAfter(new CsrfHeaderFilter(), CsrfFilter.class) 
      .csrf() 
      .csrfTokenRepository(csrfTokenRepository()); 
     } 

     private CsrfTokenRepository csrfTokenRepository() { 
      HttpSessionCsrfTokenRepository repo = new HttpSessionCsrfTokenRepository(); 
      repo.setHeaderName("X-XSRF-TOKEN"); 
      return repo; 
     } 
    } 
} 

そして終了する、UserDetailsS​​ervice

@Service 
public class UserDetailsServiceImpl implements UserDetailsService { 

    public UserDao userDao; 

    @Autowired 
    public UserDetailsServiceImpl(UserDao _userDao) { 
     super(); 
     userDao = _userDao; 
    } 

    @Override 
    public UserDetails loadUserByUsername(String arg0) throws UsernameNotFoundException { 

     UserDetailsImpl userDetailsImpl = null; 

     User user = userDao.findByLogin(arg0); 

     if(user == null){ 
      throw new UsernameNotFoundException("Login not found"); 
     } else{ 
      userDetailsImpl = new UserDetailsImpl(user); 
     } 

     return userDetailsImpl; 
    } 

} 




public class UserDetailsImpl implements UserDetails { 

    /** The Constant serialVersionUID. */ 
    private static final long serialVersionUID = 1L; 

    /** The _username. */ 
    private String username; 

    /** The _password. */ 
    private String password; 

    public UserDetailsImpl(User user) { 
     username = user.getLogin(); 
     password = user.getPwd(); 
    } 

    /** 
    * @param password the password to set 
    */ 
    public void setPassword(String password) { 
     this.password = password; 
    } 

    @Override 
    public Collection<? extends GrantedAuthority> getAuthorities() { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public String getPassword() { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    /** 
    * @param username the username to set 
    */ 
    public void setUsername(String username) { 
     this.username = username; 
    } 

    @Override 
    public String getUsername() { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public boolean isAccountNonExpired() { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public boolean isAccountNonLocked() { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public boolean isCredentialsNonExpired() { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public boolean isEnabled() { 
     // TODO Auto-generated method stub 
     return false; 
    } 

} 

についての私のJavaのあなたのアイデアを持っていますか?ありがとう

+0

あなたの質問にコードを追加してください、あなたが追加情報を得るためにリンクをクリックするのを手伝ってくれる人には許さないでください。 –

+0

私は私の投稿を編集:)ありがとう。 – Paladice

答えて

1

このウィンドウには、基本認証ウィンドウで、あなたのコードでは、基本認証(httpBasic)を使用している:あなたが保護されたアクセスしようとしている場合

http.httpBasic() 
    .and() 
    .authorizeRequests() 
    .antMatchers("/index.html","/home.html","/login.html","/").permitAll() 
    .anyRequest() 
    .authenticated() 

だから、基本認証を適用するために、あなたの春のセキュリティを設定しますURL。

この設定に基づいて、認証ヘッダー(ユーザーの資格情報を含む)が含まれている場合、Chromeはhttp要求をチェックします。認証ウィンドウが開かれていない場合はクロムが認証ウィンドウを開き、ユーザー名とパスワードを挿入します。ウィンドウの問題を解決するために

あなたは以下のコードで書いたように、あなたは(このコードは正常に動作している場合は、クロムの開発者ツールでリクエストヘッダを監視することで確認してください)angularJSコードで基本認証を処理する必要が

var headers = credentials ? {authorization : "Basic " + btoa(credentials.username + ":" + credentials.password)} : {}; 

     $http.get('user', {headers : headers}).then(
      function(response) { 
       if (response.data.name) { 
        $rootScope.authenticated = true; 
       } else { 
        $rootScope.authenticated = false; 
       } 
       callback && callback(); 
      }, 
      function() { 
       $rootScope.authenticated = false; 
       callback && callback(); 
      } 
     ); 

注意: Advanced Rest Client Toolを使用している場合、Basic認証ヘッダーを渡すことができます。このヘッダーを渡さないと、認証ウィンドウは開かれません。

+0

ありがとう= D私はあなたの助けで問題を渡します。再度ありがとう:) – Paladice

+0

あなたは歓迎です –

0

クロムは、信頼できるサイトの設定をInternet Explorerと同じインターネットオプションから取得します。あなたはここにChromeからダイアログにアクセスすることができます

のプロパティ] - > [詳細設定を表示 - >プロキシ設定を変更] - > [セキュリティ] - > [信頼済みサイト

に注意してください:あなたがアクセス最初の時間にログインするよう要求されます信頼できるサイト

+0

私はそれが春の問題であり、ブラウザの問題ではないと考えています;) – Paladice

0

/check-loginはあなたのログインページです。 yesの場合 -

このURLは、antmatcher( "/ check-login")、permitAll()も参照する必要があります。

/check-login 

"check-login" urlがログインに使用されます。 permitAllを内部に入れない場合、/ check-loginページにも認証が必要です。これにより、Spring Securityの認証が無効になります。

+0

いいえページではありません。これは私のREST WebサービスのURLです:)。 – Paladice

関連する問題