java - Spring 4安全规则定义

标签 java spring-mvc spring-security

我已将 Spring Security 从 3x 更新到 4.0.1.RELEASE。然后我终于进行了更改,完全删除旧的 XML,并将其替换为纯 java 配置。但我的安全措施无法正常工作。

问题:

  • 我的默认登录页面未授权,在 POST/login.htm 下我有 404。
  • 我的主应用程序可以未经授权运行
  • 因为登录 POST 时出现 404,所以我没有进入 UserDetailsS​​ervice
  • 所有 bean 都提供到此配置中,我没有上下文启动问题

我的配置文件:

@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");
    }

}

有人可以帮我解决这个问题吗?我已经看过一些 no-xml 配置,但它们似乎不适用于我的示例。

我的代码来源可以找到here .

最佳答案

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

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:

Granting access to the formLogin() URLs is not done by default since Spring Security needs to make certain assumptions about what is allowed and what is not. To be secure, it is best to ensure granting access to resources is explicit.

关于java - Spring 4安全规则定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31126039/

相关文章:

java - Spring安全permitall()适用于@RestController但不适用于@Controller

java - 将项目添加到链接列表的末尾

java - 为什么我无法将我的枚举声明为公共(public)枚举?

具有多个值的 Java hashmap

java - 自动检测挂线

java - 运行服务器时出现 BeanCreationException

java - 如何使用 javascript 将对象传递给 Controller

grails - grails spring安全插件-为什么AjaxAwareAuthenticationSuccessHandler总是删除保存的请求?

java - 获取 java.lang.IllegalStateException : Neither BindingResult nor plain target object for bean name 'command' available as request attribute

java - 创建 WebSecurityConfiguration 中定义的名称为 'springSecurityFilterChain' 的 bean 时出错