私は、spring-boot + spring-mvc + spring-securityプロジェクトをセットアップしました。無効なURLをログインページにリダイレクトするためのスプリングセキュリティの防止
無効なURLを除いて、現在すべてが正常に動作します。私が発行した場合
:
私は404ページが見つかりませんを見ることを期待しますが、春・セキュリティは、最初のログインページにリダイレクトし、認証後に404が見つからない示しています!
私はこれがうまくいくとは思わない!ここで
は私のWebSecurity社の設定は次のとおりです。
package com.webitalkie.webapp.config;
import java.util.EnumSet;
import javax.servlet.DispatcherType;
import javax.servlet.ServletContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
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.password.PasswordEncoder;
import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
import com.webitalkie.webapp.security.DatabaseUserDetailsServic;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private DatabaseUserDetailsServic userDetailsService;
@Autowired
private ServletContext servletContext;
@Override
protected void configure(HttpSecurity http) throws Exception {
servletContext.getFilterRegistration(AbstractSecurityWebApplicationInitializer
.DEFAULT_FILTER_NAME).addMappingForUrlPatterns(EnumSet
.allOf(DispatcherType.class), false, "/*");
http.csrf().disable();
http.authorizeRequests()
.antMatchers("/home/", "/home", "/home/index", "/", "/favicon.ico").permitAll()
.antMatchers("/contents/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/home/loginsec").failureUrl("/loginsecerror").permitAll()
.and()
.logout()
.logoutUrl("/home/logout")
.logoutSuccessUrl("/home/index")
.invalidateHttpSession(true);
}
@Override
@org.springframework.beans.factory.annotation.Autowired
protected void configure(
org.springframework.security.config.annotation
.authentication.builders.AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(getPasswordEncoder());
}
public PasswordEncoder getPasswordEncoder() {
return new BcryptPasswordEncoder(11);
}
}
君たちは、任意のアイデアを持っていますか?
最後の部分は本当に助けにはならない、OPはすべての既存のURLをリストすることはできません。認証済みのユーザは404の代わりに403を受け取るべきではないので、 '.anyRequest()。anonymous()'の代わりに '.anyRequest()。permitAll()'を使用します。 – dur
ありがとう@willerr、実際に私はセキュリティの前に無効なパスをチェックするソリューションですが、これは実際に私のユースケースを解決します。ありがとうございました – madz