Angular 4.0 + Spring Boot + Spring Security:TemplateInputException:解析模板“登录"时出错 [英] Angular 4.0 + Spring boot + Spring Security: TemplateInputException: Error resolving template "login"

查看:203
本文介绍了Angular 4.0 + Spring Boot + Spring Security:TemplateInputException:解析模板“登录"时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Spring Security集成到我的第一个Angular项目中.我关注了许多文章和示例,包括

I am integrating Spring Security in my first Angular project. I followed many articles and examples including this, this, this, this and this. I want my application to have spring security and authenticate from LDAP. If I keep a login.html in \src\main\resources\templates then I can see the login page. But I want the login page to come from Angular component. So I removed/rename the login.html from \src\main\resources\templates. When I hit localhost:8080, it goes to my LoginController (yu=ou can see the sysout in the log), Then I am getting below error

018-04-28 07:30:34.610 DEBUG 11520 --- [nio-8080-exec-9] o.s.s.w.a.i.FilterSecurityInterceptor    : RunAsManager did not change Authentication object
2018-04-28 07:30:34.610 DEBUG 11520 --- [nio-8080-exec-9] o.s.security.web.FilterChainProxy        : /login reached end of additional filter chain; proceeding with original chain
>>>>>>>>>>>> LoginController.login()...
2018-04-28 07:30:34.611 ERROR 11520 --- [nio-8080-exec-9] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-8080-exec-9] Exception processing template "login": Error resolving template "login", template might not exist or might not be accessible by any of the configured Template Resolvers
2018-04-28 07:30:34.611 DEBUG 11520 --- [nio-8080-exec-9] w.c.HttpSessionSecurityContextRepository : SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.
2018-04-28 07:30:34.611 DEBUG 11520 --- [nio-8080-exec-9] s.s.w.c.SecurityContextPersistenceFilter : SecurityContextHolder now cleared, as request processing completed
2018-04-28 07:30:34.612 ERROR 11520 --- [nio-8080-exec-9] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "login", template might not exist or might not be accessible by any of the configured Template Resolvers] with root cause

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "login", template might not exist or might not be accessible by any of the configured Template Resolvers
    at org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:246)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060)

在谷歌搜索后,我尝试了很多方法,但是还没有走运.有任何建议..

I tried many ways after Googling, but no luck yet. Any suggestion please..

我的文件:

LoginController.java

LoginController.java

 @RequestMapping("/login")
    String login(){
        System.out.println(">>>>>>>>>>>> LoginController.login()...");
        return "login";
    }

WebSecurityConfiguration.java

WebSecurityConfiguration.java

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        //.httpBasic().and()
        .authorizeRequests()
      //  .antMatchers("/**").permitAll()
        .antMatchers("/index.html", "/", "/home", "/login").permitAll()
            .anyRequest().authenticated()
            .and().formLogin().loginPage("/login")
            .and()
        .csrf()
            .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
        /*http
                .authorizeRequests()
                .antMatchers(HttpMethod.GET, "/resources/**", "/assets/**", "/css/**", "/js/**", "/fonts/**")
              //.antMatchers("/**")
                .permitAll()
                .and()
                .authorizeRequests()
                .anyRequest()
                .authenticated()
                .and()
                .formLogin()
                .loginPage("/login")
                .successHandler(authenticationSuccessHandler)
                .permitAll()
                .and()
                .logout()
                .logoutUrl("/logout")
                .and()
                .csrf()
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                .and()
                .httpBasic();
                //http.cors();
*/    }

app-routing.module.ts

app-routing.module.ts

const routes: Routes = [
    { path: 'login', component: LoginComponent },    
    { path: '', redirectTo: '/home', pathMatch: 'full' },
    { path: 'dashboard', redirectTo: '/home', pathMatch: 'full' },
    {
        path: 'home',
        component: DashboardComponent,
        data: { title: 'Dashboard' }
    },
    {
        path: 'linesidemonitor',
        component: LinesideMonitorComponent,
        data: {title: 'Lineside Monitor'}
    },


如果我使用.and().formLogin(),则出现此错误:


If I use .and().formLogin() then I am getting this error:

2018-04-28 08:32:08.394 DEBUG 10404 --- [nio-8080-exec-6] o.s.s.w.a.ExceptionTranslationFilter     : Access is denied (user is anonymous); redirecting to authentication entry point

org.springframework.security.access.AccessDeniedException: Access is denied
    at org.springframework.security.access.vote.AffirmativeBased.decide(AffirmativeBased.java:84)
    at org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:233)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:124)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)


如果我使用.httpBasic().and(),那么我会得到我不想要的浏览器登录弹出窗口


If I use .httpBasic().and(), then I am getting browser login popup which I don't want

推荐答案

@SK

出现以下异常的原因是您使用的是Controller而不是RestController.

The reason you are getting the below exception is you are using Controller and not RestController.

org.thymeleaf.exceptions.TemplateInputException:解决模板登录"时出错,模板可能不存在,或者任何已配置的模板解析器都无法访问该模板 在org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:246) 在org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104)

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "login", template might not exist or might not be accessible by any of the configured Template Resolvers at org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:246) at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104)

因此,与其将输出作为JSON直接写入浏览器,不如执行ViewResolver来解析MVC的视图部分.

So instead of directly writing the output to browser as JSON, it is doing ViewResolver to resolve the view part of MVC.

用RestController替换Controller将使.formLogin().loginPage("/login")进行浏览器登录请求,并且在允许的情况下,您将获得angular的登录页面.

Replacing Controller with RestController will make the .formLogin().loginPage("/login") to make the browser request with login, and as it permitted you will get the login page of angular.

httpBasic,它是弹出式浏览器的本质,因为它获得了401响应代码.

httpBasic, it is the nature of browser for pop up as it gets a 401 response code.

这篇关于Angular 4.0 + Spring Boot + Spring Security:TemplateInputException:解析模板“登录"时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆