簡體   English   中英

如何為特定的URL啟用Spring安全性

[英]How to enable spring security for particular url

即時通訊使用春季安全,我的配置是

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.
        authorizeRequests()
            .antMatchers("/**").permitAll()
            .antMatchers("/admin/**").hasAuthority("ADMIN").anyRequest().authenticated()
            .and().csrf().disable().formLogin().loginPage("/adminlogin").failureUrl("/adminlogin?error=true")
            .defaultSuccessUrl("/admin/dashboard")
            .usernameParameter("email")
            .passwordParameter("password")
            .and().logout()
            .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
            .logoutSuccessUrl("/adminlogin?logout=true").and().exceptionHandling()
            .accessDeniedPage("/accessdenied");
}

現在,我正在嘗試實現所有鏈接都可以訪問而沒有任何安全性,但是以/ admin / **開頭的鏈接僅允許角色為“ admin”的用戶使用。

但現在,它允許所有人使用/ admin / **。

有什么建議么。

我已經嘗試過stackoverflow的許多解決方案,即如何修復Spring Security中的角色? 但沒有運氣。 行為保持不變,它甚至允許/ admin / url公開使用。

為什么不嘗試角色?

@Configuration
@EnableWebSecurity
public class SecSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
          .authorizeRequests()
          .antMatchers("/admin/**").hasRole("ADMIN");
    }
   }

參考: http : //www.baeldung.com/spring-security-expressions-basic

http
    .csrf().disable()      
    .httpBasic().and()
    .authorizeRequests()
        .antMatchers("/admin/**").hasAuthority("ADMIN")
        .antMatchers("/**").permitAll()
        .anyRequest().authenticated()
        .and()
    .formLogin()
    .loginPage("/adminLogin").failureUrl("/adminLogin?error=true")
    .defaultSuccessUrl("/admin/dashboard")
    .usernameParameter("email")
    .passwordParameter("password")
    .and().logout()
    .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
    .logoutSuccessUrl("/adminLogin?logout=true").and().exceptionHandling()
    .accessDeniedPage("/accessdenied");

非常適合我。

暫無
暫無

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

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