首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring 4安全规则定义

Spring 4安全规则定义
EN

Stack Overflow用户
提问于 2015-06-29 21:39:39
回答 1查看 150关注 0票数 1

我已经将spring安全性从3x更新到了4.0.1.RELEASE。然后,我终于进行了更改,完全删除了旧的XML,并将其替换为纯java配置。但我的保安没能正常工作。

问题:

  • 我的默认登录页面没有授权,在POST /login.htm下我有404。
  • 我的主应用程序可以以未经授权的方式运行。
  • 因为我在登录帖子上有404,所以我不会进入UserDetailsService
  • 所有bean都被提供到此配置中,我没有上下文启动问题。

我的配置文件:

代码语言:javascript
复制
@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配置,但它们似乎并没有在我的示例中工作。

我的代码的源代码可以找到这里

EN

回答 1

Stack Overflow用户

发布于 2015-06-30 03:11:59

我认为修复很简单(我希望)您没有建议任何人可以访问您的登录表单:

代码语言:javascript
复制
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需要对允许的和不允许的进行某些假设。为了安全起见,最好确保对资源的访问是明确的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31126039

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档