[英]Spring secure both web app and rest app
我讀了很多關於如何配置spring安全性的手冊,但是仍然對配置有所保留。 所以我想配置rest呼叫和其他http呼叫。 據我了解,我可以為Web應用程序創建/ server / **之類的URL,為rest應用程序創建/ rest / **之類的URL。 對於Web應用程序URL的任何調用,我想創建一個登錄頁面(當用戶未通過身份驗證時),但是對於其余應用程序,我想觸發未授權的代碼。
我通過擴展WebSecurityConfigurerAdapter來使用Spring注釋
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().authorizeRequests()
.antMatchers("/").access("hasRole('ROLE_USER')")
.antMatchers("/server/*").access("hasRole('ROLE_USER')")
.and()
.formLogin().loginPage("/login").permitAll().failureUrl("/login?error")
.usernameParameter("username")
.passwordParameter("password")
.and()
.exceptionHandling()
.accessDeniedPage("/accessDenied");
}
對於服務器來說,它工作正常,但是如果我嘗試在瀏覽器中調用/ rest / [something]時在此處添加/ rest,它將始終將我轉發到/ login頁面。 我不明白為什么,這讓我很沮喪。 感謝您提供任何有用的回復。
你有這個:
.antMatchers("/").access("hasRole('ROLE_USER')")
.antMatchers("/server/*").access("hasRole('ROLE_USER')")
.and()
.formLogin()
意味着/訪問需要ROLE_ACCESS並進行身份驗證,直接發送至formLogin
您需要多個身份驗證配置。
參見http://docs.spring.io/autorepo/docs/spring-security/4.0.0.CI-SNAPSHOT/reference/htmlsingle/
在其中之一中,您需要像這樣的東西
http
.antMatcher("/rest/**")
.exceptionHandling().authenticationEntryPoint(new AccessDenyEntryPoint()).and()
.authorizeRequests().antMatchers("/spring/**").denyAll();
如果您不想驗證其余網址,則必須再添加一個.antMatchers("/rest/*").permitAll()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.