春ブーツで基本認証でのRESTfulを使用する方法

エルビス:

Helllo、私は基本認証でのRESTfulを使用していますし、このコードはRestControllerからの一部です:

@GetMapping("/jpa/users/{username}/goals")
public List<Goal> getAllGoals(@PathVariable String username) {
    userId = getUserIdFromUsername(username);
    return goalJpaRepository.findByUserId(userId); 
}

public Long getUserIdFromUsername(String username) {
    User user = userJpaRepository.findByUsername(username);
    userId = user.getId(); 
    return userId;
}

そして、私は問題を抱えている、例えば私はこのようなspecifficユーザーのための目標を取得するために、ポストマンを使用しています:

http:// localhost:8080 / JPA /ユーザー/ジョン/ゴール GET要求に

それから私は、ユーザー名ジョンと、このユーザ名のパスワードを基本認証を使用して、私はジョンの目標を受け取ります。

その私がこのリンクのGET要求を行う場合の後に// localhostを:8080 / JPA /ユーザー/トム/目標のhttpを、私はトムの目標を受けるが、私は時間のこの時点で、ジョンとログインしていますので、ジョン・缶彼のゴールを見ても、彼はトムのゴールを見ることができます。

質問は、私はこのような何かをしたいので、私は、RestControllerにログインユーザ名にアクセスする方法です。

if (loginUsername == username) {
    return goalJpaRepository.findByUserId(userId);
} 

return "Access denied!";

だから私はHTTPヘッダーからログインユーザ名にアクセスすることが可能であるかどうかを知りたいですか?

ありがとうございました!


UPDATE -はいフレームワークは、私は、MySQLデータベースからユーザーを取得したいので、また、私はダオ認証で春のセキュリティを使用しています、春ブーツです。とにかく私は春のセキュリティの専門家ではありません。

今、私は私のコントローラメソッドで主に使用する方法を理解するが、私は、この特定のケースのために春のセキュリティを使用する方法がわかりません。どのように私はそれを実装する必要がありますか?例えば、ユーザが、ジョンは彼の目標を確認し、変更する必要があります。

春のセキュリティ設定:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import com.dgs.restful.webservices.goaltrackerservice.user.MyUserDetailsService;

@Configuration
@EnableWebSecurity
public class SpringSecurityConfigurationBasicAuth extends WebSecurityConfigurerAdapter {

    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Autowired
    private MyUserDetailsService userDetailsService;

    @Bean
    public DaoAuthenticationProvider authenticationProvider() {
        DaoAuthenticationProvider authProvider
          = new DaoAuthenticationProvider();
        authProvider.setUserDetailsService(userDetailsService);
        authProvider.setPasswordEncoder(bCryptPasswordEncoder());
        return authProvider;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http
            .csrf().disable()
            .authorizeRequests()
            .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
            .antMatchers("/allusers").permitAll()
                .anyRequest().authenticated()
                .and()
            // .formLogin().and()
            .httpBasic();
        }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authenticationProvider());
    }
}
gervais.b:

あなたはこの時点でどのようなセキュリティをしていないことに注意してください。

@Mattで言ったように「それはあなたが使用しているフレームワーク依存します」しかし、私は、あなたが春を使用していると思います。その後、春-securutyモジュールのドキュメントを見ている必要があります。

基本的に、あなたのメソッドのパラメータに認証されたユーザを注入することができます:

   @GetMapping("/jpa/users/{username}/goals")
   public List<Goal> getAllGoals(@PathVariable String username, Principal principal) {
     if ( username.equals(principal.getName()) ) {
       userId = getUserIdFromUsername(username);
       return goalJpaRepository.findByUserId(userId); 
     } else {
       throw new SomeExceptionThatWillBeMapped();
     }
   } 

しかし、春・セキュリティおよび多くのフレームワークは、セキュリティを管理するためのより良いパターンを提供します。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=203611&siteId=1