dashboard.html
というページへのアクセスを認証されていないユーザーに制限しようとしています。これまでのところ、私は成功しなかった。ここでのマイWebSecurityConfigurerAdapter
:私はそれは私がログインしようとするたびに、無限のリダイレクトループが発生し、このように設定しているときはいつでもSpringブートRESTを使用して.htmlページへのアクセスを制限する方法
@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Autowired
private CustomAuthenticationSuccessHandler authenticationSuccessHandler;
@Autowired
private CustomAuthenticationFailureHandler authenticationFailureHandler;
@Autowired
private CustomUserDetailsService userDetailsService;
@Autowired
private TokenAuthenticationService tokenAuthenticationService;
@Bean
public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Override
protected void configure(AuthenticationManagerBuilder builder) throws Exception {
builder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/index.html", "/",
"/login.html","/signup.html", "/videos/**",
"/login", "/logout", "/images/**", "/fonts/**",
"/css/**", "/js/**", "/pages/**", "/sass/**"
).permitAll()
.and()
.authorizeRequests()
.antMatchers("/dashboard/**", "/dashboard.html/**").authenticated()
.and()
.authorizeRequests()
.anyRequest().authenticated()
.and()
.addFilterBefore(new StatelessLoginFilter("/login", tokenAuthenticationService, userDetailsService, authenticationManager()), UsernamePasswordAuthenticationFilter.class)
.addFilterBefore(new StatelessAuthenticationFilter(tokenAuthenticationService), UsernamePasswordAuthenticationFilter.class)
.formLogin()
.loginPage("/login.html")
.loginProcessingUrl("/login")
.usernameParameter("email")
.passwordParameter("password")
.successHandler(authenticationSuccessHandler)
.failureHandler(authenticationFailureHandler)
.and()
.logout()
.logoutSuccessUrl("/")
.deleteCookies("JSESSIONID")
.permitAll()
.and()
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.and()
.addFilterAfter(new CsrfTokenFilter(), CsrfFilter.class);
}
}
。ブラウザはdashboard.html
にナビゲートしようとしますが、制限されています。これにより、有効なトークンがあるため、ダッシュボードにリダイレクトしようとするログインページにリダイレクトされます。
私はそれは以下のように設定している場合は、誰もがdashboard.html
にアクセスし、希望されていない/dashboard
エンドポイントに電話をかけることができます:
http
.authorizeRequests()
.antMatchers("/index.html", "/",
"/login.html","/signup.html", "/videos/**",
"/login", "/logout", "/images/**", "/fonts/**",
"/css/**", "/js/**", "/pages/**", "/sass/**",
"/dashboard/**", "/dashboard.html/**").permitAll()
.and()
.authorizeRequests()
.anyRequest().authenticated()
私のログインはJWTトークンを使用し、SecurityContext
を設定するには、以下のフィルタを使用していますプレースホルダ:
私はSecurityContextHolder.getContext().setAuthentication(userAuthentication);
という行を使って認証を設定しています。これは完全に正常に動作します。ユーザーが、ユーザーから送信された信任状と一致するDB内に見つかった場合、セキュリティー・コンテキストを使用して、ユーザーに関連付けられたさまざまなデータを検索できます。
私の質問:がどのようにページdashboard.html
を制限し、認証されていないユーザー(SecurityContextHolder
内部の認証オブジェクトのないもの)に/dashboard
エンドポイントに呼び出しをすることができますか?
許可をすべて "許可する"ことをお確かめください。おそらくすべてが許可されます。 – kuhajeyan