【问题标题】:Diffrent way of implementation security ends with the same error不同的实现方式安全性以相同的错误结束
【发布时间】:2017-11-14 22:54:02
【问题描述】:

我已经实现了一个结合Spring BootAngular 4 的应用程序。我把所有Angular文件放在/resources/static目录下:

static directory

然后我添加到Spring Security 类:

@Configuration
@EnableWebMvc
@ComponentScan("com.inventory")
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/static/**")
                .addResourceLocations("classpath:/resources/static/");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index.html");
    }
}

和:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordencoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().authorizeRequests()
                .antMatchers("/").permitAll()
                .anyRequest().authenticated();
    }

似乎一切都应该工作。不幸的是,每当我运行我的应用程序时,它都会引发异常:

There was an unexpected error (type=Internal Server Error, status=500).
Could not resolve view with name 'index.html' in servlet with name 'dispatcherServlet'.

当然我尝试了不同的解决方案,比如添加这个:

@Controller
public class ViewController {

    @RequestMapping(value = "/#/")
    public String index() {
        return "forward:/index.html";
    }

    @RequestMapping(value = "/")
    public String home() {
        return "forward:/index.html";
    }
}

但是没有任何效果。有谁知道我还能做什么?

【问题讨论】:

    标签: spring security spring-security static-content


    【解决方案1】:

    在正常使用 Spring boot 时,无需手动配置资源处理器,spring boot 会自动从以下位置加载内容:

    • /静态
    • /公共
    • /资源
    • /META-INF/资源

    spring boot guides on loading static contents

    【讨论】:

    • 如果您想根据您的情况提供自定义位置,请将其包含在您的处理程序中:.addResourceHandler("/resources/**")
    • 所以我改为:@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**", "/", "/#/").addResourceLocations("/resources/static/index.html","/resources/static/index.html","/resources/static/index.html"); } 并抛出 There was an unexpected error (type=Not Found, status=404). No message available
    • 我不认为你可以在你的 url 中使用 # 登录,它并不意味着要传入服务器。
    猜你喜欢
    • 2016-05-15
    • 2018-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多