我創建了一個Spring Boot應用程序,並在/resources/static
文件夾中有我的前端應用程序。用Spring Security保護Angular JS應用程序
對於路由,我使用Angular JS UI路由器庫。 我已經定義了一個路由,我只想訪問管理員,現在我試圖使用Spring Security來保護它。
這裏是我WebSecurity配置類:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER").and()
.withUser("admin").password("password").roles("USER", "ADMIN");
}
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.authorizeRequests()
.antMatchers(HttpMethod.GET, "/#/admin").hasRole("ADMIN")
.and()
.formLogin()
.and()
.authorizeRequests()
.antMatchers(HttpMethod.POST, "/member", "/member/**").permitAll()
.antMatchers(
HttpMethod.GET,
"/",
"/*.html",
"/favicon.ico",
"/**/*.html",
"/**/*.css",
"/**/*.js",
"/**/**/*.css",
"/**/**/*.js",
"/**/**/*.html",
"/**/**/**/*.css",
"/**/**/**/*.js",
"/**/**/**/*.html",
"/**/**/**/**/*.css",
"/**/**/**/**/*.js"
).permitAll()
.antMatchers("/auth/**", "/member/**", "/account/**").permitAll()
.and()
.csrf().disable();
}
}
我想,以確保這條路線可以通過http://localhost:8080/#/admin訪問。
但是,每當我訪問該路由時,都不會請求登錄,任何人都可以查看該頁面。
我應該遵循另一種方法嗎?
這工作,但現在我應該登錄訪問所有的端點,即使他們之前可用 –