簡體   English   中英

Spring架構的Spring Security

[英]Spring Security with REST architecture

我一直在研究REST API的Spring Security問題。 在開始實施之前,我想在github上獲得專家建議或一些示例項目(如果有)。

我的應用程序將基於REST API。 並將由兩個客戶訪問:

  1. 移動電話
  2. 卷筒紙

如果我使用自定義登錄頁面創建REST API,那么它將始終重定向到Web(根據我的理解)。 什么時候我會開始用手機消費它?

 .formLogin()
                .defaultSuccessUrl("/ui/index.html#/app/dashboard")
                .loginProcessingUrl("/api/upuser/verifyUser")
                .usernameParameter("username")
                .passwordParameter("password")
                .successHandler(new AjaxAuthenticationSuccessHandler(new SavedRequestAwareAuthenticationSuccessHandler()))
                .loginPage("/ui/index.html#/access/signin")

我認為從上面的代碼看來,很顯然,可以從兩個不同的位置訪問此應用程序:

  1. API的localhost:8080 / api /
  2. 本地主機:8383 / ui /用於WEB(Angular JS)

但是,我將使用nginx將它們移動到localhost / api /&localhost / ui /。 因此,以上兩個將被訪問

  1. 本地主機/ API /
  2. 本地主機/ UI /

因此,我的第二個問題是實現彈簧安全性的最佳方法是什么:

  1. 基於令牌的認證
  2. 基於會話的認證

問題在於它是無狀態服務,因此我們將如何實現基於會話的身份驗證?

嘗試這樣的事情:

You should try this, may be it will help you:

@Configuration
@EnableWebSecurity
@Profile("container")
public class SOAPSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private AuthenticationProvider authenticationProvider;

@Autowired
private AuthenticationProvider authenticationProviderDB;


@Override
@Order(1)

protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.authenticationProvider(authenticationProvider);
}


@Order(2)
protected void ConfigureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.authenticationProvider(authenticationProviderDB);
}

@Override
  public void configure(WebSecurity web) throws Exception {
    web
      .ignoring()
         .antMatchers("/scripts/**","/styles/**","/images/**","/error/**");
  }

@Override
public void configure(HttpSecurity http) throws Exception {
    http
            .authorizeRequests()
            .antMatchers("/rest/**").authenticated()
            .antMatchers("/**").permitAll()
            .anyRequest().authenticated()
            .and()
            .formLogin()
            .successHandler(new AuthenticationSuccessHandler() {
                @Override
                public void onAuthenticationSuccess(
                        HttpServletRequest request,
                        HttpServletResponse response,
                        Authentication a) throws IOException, ServletException {
                            //To change body of generated methods,
                            response.setStatus(HttpServletResponse.SC_OK);
                        }
            })
            .failureHandler(new AuthenticationFailureHandler() {

                @Override
                public void onAuthenticationFailure(
                        HttpServletRequest request,
                        HttpServletResponse response,
                        AuthenticationException ae) throws IOException, ServletException {
                            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
                        }
            })
            .loginProcessingUrl("/access/login")
            .and()
            .logout()
            .logoutUrl("/access/logout")                
            .logoutSuccessHandler(new LogoutSuccessHandler() {
                @Override
                public void onLogoutSuccess(
                        HttpServletRequest request, 
                        HttpServletResponse response, 
                        Authentication a) throws IOException, ServletException {
                    response.setStatus(HttpServletResponse.SC_NO_CONTENT);
                }
            })
            .invalidateHttpSession(true)
            .and()
            .exceptionHandling()
            .authenticationEntryPoint(new Http403ForbiddenEntryPoint())
            .and()
            .csrf()//Disabled CSRF protection
            .disable();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM