如何解决 Spring 安全无需登录即可访问 url
how to solve Spring Security can access url without login
您好,我已经创建了一个示例 spring mvc 安全 application.I 遵循 java 基于代码的配置,而不是 xml configuration.The 应用程序工作正常。
但是,用户可以在不登录应用程序的情况下访问每个 url。我该如何解决这个问题??
我想,如果没有登录过程,用户不能访问 url。
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private CustomUserDetailsService customUserDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.headers()
.addHeaderWriter(new XFrameOptionsHeaderWriter(XFrameOptionsHeaderWriter.XFrameOptionsMode.SAMEORIGIN)).and()
.formLogin().defaultSuccessUrl("/admin/home")
.loginPage("/login").failureUrl("/login?error")
.permitAll().and().logout()
.logoutSuccessUrl("/login?logout").logoutUrl("/logout")
.permitAll().and().authorizeRequests().antMatchers("/**")
.permitAll().anyRequest().authenticated().and();
}
/*
* @Override protected void configure(AuthenticationManagerBuilder auth)
* throws Exception
*/
@Override
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth.userDetailsService(customUserDetailsService);
}
}
我认为这是问题所在:
.authorizeRequests()
.antMatchers("/**").permitAll()
.anyRequest().authenticated()
规则的顺序很重要(它应该首先是最具体的,最不具体的最后),因此 /**
模式将匹配所有内容,而 anyRequest().authenticated()
永远不会生效(因此访问所有 URL 都将被允许)。所以解决方案是删除 /**
规则或使其不那么通用,具体取决于您希望它做什么。
顺便说一句,你真的应该遵循 recommended conventions for indenting Spring Security Java configuration,这会让你的配置更容易阅读。
首先包括
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("admin").password("password").roles("ADMIN"); // admin in your case
}
@Bohuslav Burghardt 建议使用
http
.authorizeRequests()
.antMatchers("/resources/**", "/login").permitAll()
.antMatchers("/admin/**").hasRole("ADMIN")
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/admin/home")
.failureUrl("/loginfailed")
.permitAll()
.and()
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.deleteCookies("JSESSIONID")
.invalidateHttpSession( true )
.and();
您好,我已经创建了一个示例 spring mvc 安全 application.I 遵循 java 基于代码的配置,而不是 xml configuration.The 应用程序工作正常。 但是,用户可以在不登录应用程序的情况下访问每个 url。我该如何解决这个问题??
我想,如果没有登录过程,用户不能访问 url。
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private CustomUserDetailsService customUserDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.headers()
.addHeaderWriter(new XFrameOptionsHeaderWriter(XFrameOptionsHeaderWriter.XFrameOptionsMode.SAMEORIGIN)).and()
.formLogin().defaultSuccessUrl("/admin/home")
.loginPage("/login").failureUrl("/login?error")
.permitAll().and().logout()
.logoutSuccessUrl("/login?logout").logoutUrl("/logout")
.permitAll().and().authorizeRequests().antMatchers("/**")
.permitAll().anyRequest().authenticated().and();
}
/*
* @Override protected void configure(AuthenticationManagerBuilder auth)
* throws Exception
*/
@Override
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth.userDetailsService(customUserDetailsService);
}
}
我认为这是问题所在:
.authorizeRequests()
.antMatchers("/**").permitAll()
.anyRequest().authenticated()
规则的顺序很重要(它应该首先是最具体的,最不具体的最后),因此 /**
模式将匹配所有内容,而 anyRequest().authenticated()
永远不会生效(因此访问所有 URL 都将被允许)。所以解决方案是删除 /**
规则或使其不那么通用,具体取决于您希望它做什么。
顺便说一句,你真的应该遵循 recommended conventions for indenting Spring Security Java configuration,这会让你的配置更容易阅读。
首先包括
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("admin").password("password").roles("ADMIN"); // admin in your case
}
@Bohuslav Burghardt 建议使用
http
.authorizeRequests()
.antMatchers("/resources/**", "/login").permitAll()
.antMatchers("/admin/**").hasRole("ADMIN")
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/admin/home")
.failureUrl("/loginfailed")
.permitAll()
.and()
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.deleteCookies("JSESSIONID")
.invalidateHttpSession( true )
.and();