簡體   English   中英

Spring 安全/登錄 - 404 未找到

[英]Spring Security /login - 404 not found

我在啟用用戶登錄頁面時遇到問題 - 找不到 404。

這是我用作應用程序安全基礎的教程

這就是配置 function 的樣子:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.cors().and().csrf().disable().authorizeRequests()
            .antMatchers(HttpMethod.POST, SIGN_UP_URL).permitAll()
            .anyRequest().authenticated()
            .and()
            .addFilter(new JWTAuthenticationFilter(authenticationManager()))
            .addFilter(new JWTAuthorizationFilter(authenticationManager()))
            // this disables session creation on Spring Security
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}

我試圖在這里簡單地添加:

.and()
.formLogin()
.loginPage("/login")
.permitAll();

並將.addFilter更改為.addFilterAfter()我仍然得到 404。

正如您在教程中看到的,這就是登錄 function 的訪問方式:

curl -i -H "Content-Type: application/json" -X POST -d '{
    "username": "admin",
    "password": "password"
}' http://localhost:8080/login

甚至可以為此目的啟用內置登錄表單嗎?

如果沒有,那里的解決方案是什么? 我是否必須在 controller 中創建 /login 端點,然后將數據發布到http://localhost:8080/login

以及添加authenticationFilter怎么樣。 是否必須在那里進行更改?

你在這里有兩個問題:

  1. JWTAuthenticationFilterUsernamePasswordAuthenticationFilter擴展而來,默認情況下響應 URL /login formLogin()也在這個 URL 中生成一個登錄表單。 因此,您有兩個地方接受/login的輸入。 如果您選擇自定義登錄頁面(通過.loginPage("/login") ),則必須在不同的 URL 中執行此操作,並為此頁面提供 HTML 視圖。 但是你說你想使用內置的登錄表單。 所以,這里又出現了一個問題:
  2. 要使用內置登錄表單,必須通過默認的/login URL 完成,因此您必須更改 JWTAuthenticationFilter 的JWTAuthenticationFilter 可以通過在AbstractAuthenticationProcessingFilter中設置自定義 URL 實現,如下所示。 這就像一個魅力,但是JWTAuthenticationFilter的實現期望輸入一個 JSON,它不是由/login表單提供的(它在 POST 中發送參數)。 因此,您必須更改JWTAuthenticationFilter.attemptAuthentication的代碼,以確定輸入是來自 JSON 主體還是參數。

我在我的環境中實現了這一點並且效果很好。 下面是代碼(只是片段):

網絡安全:

public JWTAuthenticationFilter getJWTAuthenticationFilter() throws Exception {
    final JWTAuthenticationFilter filter = new JWTAuthenticationFilter(authenticationManager());
    filter.setFilterProcessesUrl("/api/auth/login");
    return filter;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
    http.cors().and().csrf().disable().authorizeRequests()
            .antMatchers(HttpMethod.POST, SIGN_UP_URL).permitAll()
            .anyRequest().authenticated()
            .and()
            .addFilter(getJWTAuthenticationFilter())
            .addFilter(new JWTAuthorizationFilter(authenticationManager()))                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
            .formLogin()
            .loginProcessingUrl("/api/auth/login")
            .permitAll();
}

JWTAuthenticationFilter:

@Override
public Authentication attemptAuthentication(HttpServletRequest req,
                                            HttpServletResponse res) throws AuthenticationException {
    try {
        ApplicationUser creds = null;

        if (req.getParameter("username") != null  && req.getParameter("password") != null) {
            creds = new ApplicationUser();              
            creds.setUsername(req.getParameter("username"));
            creds.setPassword(req.getParameter("password"));                
        } else {
            creds = new ObjectMapper()
                    .readValue(req.getInputStream(), ApplicationUser.class);
        }

        return authenticationManager.authenticate(
                new UsernamePasswordAuthenticationToken(
                        creds.getUsername(),
                        creds.getPassword(),
                        new ArrayList<>())
        );
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

暫無
暫無

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

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