【问题标题】:Spring Security - configure HttpSecuritySpring Security - 配置 HttpSecurity
【发布时间】:2017-10-18 13:04:42
【问题描述】:

我是 Spring Security 的新手,我正在尝试使用 Spring Security 创建一个登录表单。

这是必需的场景:

1) 用户使用用户名-密码登录应用(请注意我使用的是spring Security提供的默认登录页面) 2)如果登录OK,用户去eventList.jsp 3) 如果登录为 KO(错误凭据),则会显示错误

我的 WebSecurityConfigurerAdapter 配置:

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication().withUser("amdin").password("111111").roles("USER");
}

@Override
protected void configure(HttpSecurity http) throws Exception {

    http
            .authorizeRequests()
            .anyRequest().authenticated()
            .and()
            .formLogin().defaultSuccessUrl("/eventList");
}

错误 1:如果我插入正确的凭据,我看不到 /eventList,但我收到 404 (/spring-security-helloworld-annotation/WEB-INF/pages/login.jsp)。为什么我没有重定向到 /eventList? (因为 /eventList 在我的 RequestMapping 注释中只接受 GET?

@RequestMapping(value = {"/eventList"}, method = RequestMethod.GET)

错误2:如果我尝试“手动”进入/eventList,通过在我的浏览器中将“eventList”添加到URL的末尾,我可以访问请求的页面而无需执行登录操作!!!我唯一希望在不执行登录操作的情况下访问的 URL 是登录页面本身!!!

.anyRequest().authenticated() 行不应该允许这一切!!!

我怎样才能得到我想要的?

提前 TY

【问题讨论】:

  • 你在使用 Spring Boot 吗?对于第一个错误,您是否为 /eventList 定义了控制器?对于错误 2,这确实很奇怪。在您的控制台日志中,您是否有一个带有 DefaultSecurityFilterChain 的条目?
  • 1) 我有一个/eventList的控制器(实际上,如果我尝试通过在浏览器中输入URI来访问它,我可以访问该页面)

标签: java spring jsp spring-mvc spring-security


【解决方案1】:

正确的安全链如下所示:

          http
                .authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login")
                .defaultSuccessUrl("/eventList")
                .permitAll()
                .and()
                .logout()
                .permitAll();

您忘记了 permitAll() 声明以及定义 loginPage() 希望能帮助到你!如果您需要进一步的帮助,请给我留言。

【讨论】:

    猜你喜欢
    • 2020-03-04
    • 2016-08-08
    • 2021-06-29
    • 1970-01-01
    • 2016-03-14
    • 2017-09-21
    • 2021-07-04
    • 2023-01-17
    • 2021-12-15
    相关资源
    最近更新 更多