【问题标题】:Spring Boot JS App wont work after securing rest-api with Spring Security使用 Spring Security 保护 rest-api 后,Spring Boot JS App 将无法工作
【发布时间】:2016-12-30 20:24:27
【问题描述】:

我创建了一个简单的 Spring Boot/JS 应用程序。在下一步中,我尝试实现一个用户管理功能来处理多个用户。

所以我实现了一个用户模型和控制器,并通过 Spring Security 的身份验证来保护所有的 rest-api 调用。

@Configuration
@EnableWebSecurity
@ComponentScan("package.packagename")
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
DataSource dataSource;
@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception{

    auth.jdbcAuthentication().dataSource(dataSource)
            .usersByUsernameQuery("select email, password, active from accounts where email=?")
            .authoritiesByUsernameQuery("select email, role from account_roles where email=?");
}

@Override
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
protected void configure(HttpSecurity http) throws Exception {
        http
                .formLogin().permitAll()
                .and()
                .authorizeRequests()
                .antMatchers("/index.html",  "/").permitAll()
                .anyRequest().authenticated()
                .and()
                .logout();                    
    }
}

除了这个文件,我还有 SecurityWebApplicationInitializer

public class SecurityWebApplicationInitializer
    extends AbstractSecurityWebApplicationInitializer {

public SecurityWebApplicationInitializer() {
    super(SecurityConfig.class);
}
}

我现在的问题是,如果我启动应用程序并尝试通过 localhost:8080 访问它,我将面临 404 错误。 通常应用程序即使没有登录也应该工作,并且似乎启用了 springsecurity 的应用程序无法加载资源/公共目录中的 js 内容。

阅读日志显示如下:

在名称为“dispatcherServlet”的 DispatcherServlet 中找不到带有 URI [/] 的 HTTP 请求的映射

如果我在没有 spring security 的情况下启动应用程序,它可以正常工作。

api 调用的保护就像一个魅力——我能够登录、接收一个 cookie 并使用这个 cookie 对由 springsecurity 保护的 api 函数进行身份验证。

我希望你能帮助我解决我的(希望很小)问题。

提前致谢

【问题讨论】:

    标签: spring spring-security spring-boot


    【解决方案1】:

    当您使用 .formLogin() 时,您需要定义登录页面或使用 .httpBasic() 身份验证。所以你可以使用这样的东西:

        .formLogin()                      
            .and()
        .httpBasic();
    

        .formLogin()
            .loginPage("/login.html")
    

    你可以在这里阅读更多

    http://docs.spring.io/spring-security/site/docs/3.2.x/guides/form.html

    http://www.baeldung.com/spring-security-login

    【讨论】:

    • 这不是我的问题。登录后我无法访问我的前端。主要问题是:o.s.web.servlet.PageNotFound : No mapping found for HTTP request with URI [/] in DispatcherServlet with name 'dispatcherServlet' 错误。成功登录后,我无法访问该网站 - 我只是收到 404 消息...
    • 您是否在 / URI 上配置了页面/资源?
    • 我不知道你的意思。我只是把每个前端的东西都放到了资源/公共目录中,它在没有弹簧安全的情况下工作。我错过了在 SpringSecurity 中配置的东西吗?
    • 您确定身份验证运行良好吗?我认为它可能会将您重定向到错误页面,但它无法向您显示错误页面,因为它不存在或没有权限。
    • 我可以通过 curl 进行身份验证。我收到一个 cookie,我可以用它连接到安全的 api 调用。但没有机会访问主页面 - 存储在公共目录中。
    【解决方案2】:

    我发现我必须像这样添加一个 WebConfig.java 类:

    import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    
    @Configuration
    @EnableWebMvc
    @ComponentScan
    public class WebConfig extends WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter {
    
    }
    

    现在 Spring 能够正确解析 / 调用。我只需要确保为所有用户公开对所有文件的访问(permitall() 函数)。

    无论如何感谢您的帮助:-)

    【讨论】:

      猜你喜欢
      • 2015-12-09
      • 2014-02-21
      • 2014-07-29
      • 2014-12-06
      • 2016-02-23
      • 2018-08-11
      • 2012-11-20
      • 2016-03-04
      • 2015-03-21
      相关资源
      最近更新 更多