我已经将spring安全性从3x更新到了4.0.1.RELEASE。然后,我终于进行了更改,完全删除了旧的XML,并将其替换为纯java配置。但我的保安没能正常工作。
问题:
我的配置文件:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@ComponentScan(basePackages = {"com.dal.dao.security", "com.services.security"})
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
LocalUserDao userDao;
@Autowired
UserRoleMapper roleMapper;
@Autowired
StandardPasswordEncoder passwordEncoder;
private UserDetailsService methodSecurityService() throws Exception {
return new UserDetailsServiceImpl(userDao, roleMapper);
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(methodSecurityService()).passwordEncoder(passwordEncoder);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/*")
.authenticated()
.and()
.formLogin()
.loginPage("/login.htm").failureUrl("/login.htm?error")
.usernameParameter("username")
.passwordParameter("password")
.and().logout().logoutSuccessUrl("/index.htm")
.and().csrf()
.and().exceptionHandling().accessDeniedPage("/403");
}
}有人能帮我吗?我已经注意到了很少的非xml配置,但它们似乎并没有在我的示例中工作。
我的代码的源代码可以找到这里。
发布于 2015-06-30 03:11:59
我认为修复很简单(我希望)您没有建议任何人可以访问您的登录表单:
http.authorizeRequests()
.antMatchers("/*")
.authenticated()
.and()
.formLogin()
.loginPage("/login.htm").failureUrl("/login.htm?error")
.usernameParameter("username")
.passwordParameter("password")
.permitAll() // ADD THIS LINE
.and().logout().logoutSuccessUrl("/index.htm")
.and().csrf()
.and().exceptionHandling().accessDeniedPage("/403");本质上,您是将用户重定向到登录页面,而不启用未经身份验证的访问。基本上,您要求他们进行身份验证,以查看身份验证表单:)。
来自Spring Security:
默认情况下不会授予对formLogin() URL的访问,因为Security需要对允许的和不允许的进行某些假设。为了安全起见,最好确保对资源的访问是明确的。
https://stackoverflow.com/questions/31126039
复制相似问题