[英]Spring Security HTTP Basic for RESTFul and FormLogin (Cookies) for web - Annotations
[英]Spring Security HTTP FormLogin
我的Spring Security有問題
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UsuarioService usuarioService;
@Autowired
private PasswordEncoder passwordEncoder;
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.authorizeRequests()
.antMatchers(
"/",
"/index/**",
"/register",
"/doregister",
"/login",
"/dologin",
"/logout-success",
"/confirmregister",
"/invaliduser",
"/expiredtoken",
"/userstatuslog",
"/verifyemail")
.permitAll()
.anyRequest()
.authenticated()
.and()
.authorizeRequests()
.antMatchers(
"/login",
"logout")
.permitAll()
.and()
.authorizeRequests()
.antMatchers(
"/static/css/**",
"/css/custom.css",
"/js/**",
"/images/**"
)
.permitAll()
.and()
.authorizeRequests()
.antMatchers(
"/forbidden",
"/edit-profile-about",
"/doeditprofileabout",
"/profile"
)
.hasRole("USER")
.and()
.authorizeRequests()
.antMatchers(
"/controlpanel",
"/forbidden",
"/edit-profile-about",
"/doeditprofileabout",
"/profile"
)
.hasRole("ADMIN")
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/")
.permitAll()
.and()
.logout()
.deleteCookies("remove")
.invalidateHttpSession(true)
.logoutUrl("/logout")
.logoutSuccessUrl("/logout-success")
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"));
// @formatter:on
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(usuarioService).passwordEncoder(passwordEncoder);
}
每次登錄時,它會將我發送到引導文件...
本地主機:8080 / css / bootstrap-3.3.7-dist / css / bootstrap.min.css
要么...
本地主機:8080 / css / custom.css
要么...
本地主機:8080 / js / jquery-2.1.4.min.js
但不是同一個人(似乎是隨機的!:-)
然后,有時CSS有效,有時卻無效。 在登錄表單中,它沒有樣式,但是只有某些時候,因為在我運行應用程序時,它具有樣式。 當我也登錄時,但在注銷或嘗試再次登錄時卻沒有。 因此,當用戶正確登錄后,它將再次具有樣式。
並且當我嘗試與用戶(使用ROLE_USER)訪問“ / controlpanel”時,它允許訪問(並且不應),但是當我與用戶(Role_ADMIN)一起使用時,它可以正常工作。
問題是來賓無法訪問您的css文件。 因此,登錄后,您將被重定向回它。 由於瀏覽器緩存,登錄頁面中的CSS有時會起作用。
嘗試將您的配置更改為
http
.authorizeRequests()
.antMatchers(
"/",
"/index/**",
"/register",
...).permitAll()
.antMatchers(
"/login",
"logout").permitAll()
.antMatchers( // You need to add all css file here
"/static/css/**",
"/css/custom.css",
"/css/bootstrap-3.3.7-dist/css/bootstrap.min.css",
"/js/**",
"/images/**").permitAll()
.antMatchers(
"/forbidden",
"/edit-profile-about",
"/doeditprofileabout",
"/profile").hasRole("USER")
.antMatchers(
"/controlpanel",
"/forbidden",
"/edit-profile-about",
"/doeditprofileabout",
"/profile").hasRole("ADMIN")
.anyRequest().authenticated()
.and()
...
當Spring Security過濾了您的靜態資源時,就會發生這種情況。
嘗試完全忽略這些資源:
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/path/**");
}
定義模式以滿足您的所有位置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.