2016-06-29 39 views
3

我想为我的自举应用程序添加自定义登录页面。我正在关注this教程。我无法使用自定义登录页面进行工作。春季开机自定义登录页面

这里是我的pom.xml:

... 
<dependency> 
    <groupId>org.springframework.data</groupId> 
    <artifactId>spring-data-commons</artifactId> 
</dependency> 
<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-test</artifactId> 
    <scope>test</scope> 
</dependency> 
<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-security</artifactId> 
</dependency> 
<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-thymeleaf</artifactId> 
</dependency> 
<dependency> 
    <groupId>org.apache.httpcomponents</groupId> 
    <artifactId>httpclient</artifactId> 
    <scope>test</scope> 
</dependency> 
... 

MvcConfig.java

import org.springframework.context.annotation.Configuration; 
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 

@Configuration 
public class MvcConfig extends WebMvcConfigurerAdapter { 

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

     registry.addViewController("/login").setViewName("login"); 
    } 

} 

FrontendApp.java:

import org.slf4j.LoggerFactory; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.context.annotation.Import; 

import ch.qos.logback.classic.Logger; 

@SpringBootApplication 
@Import(value = MvcConfig.class) 
public class FrontendApp { 

     private static Logger logger = (Logger) LoggerFactory.getLogger(FrontendApp.class); 

     public static void main(String[] args) { 

      SpringApplication app = new SpringApplication(FrontendApp.class); 
      app.run(args); 
    } 

} 

SecurityConfiguration.java

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true) 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { 
    @Autowired 
    private CustomAuthenticationProvider customAuthenticationProvider; 

    @Autowired 
    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.authenticationProvider(this.customAuthenticationProvider); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .authorizeRequests() 
      .antMatchers("/css/**").permitAll() 
      .antMatchers("/resources/**").permitAll() 
      .antMatchers("**").permitAll() 
      .antMatchers("/login").permitAll() 
     .anyRequest().authenticated().and() 
     .formLogin() 
     .loginPage("/login"); 
    } 

} 

我打开了所有的网址,所以我只是检查我是否可以看到/登录或不。 CustomAuthenticationProvider.java

@Component 
public class CustomAuthenticationProvider implements AuthenticationProvider {  

    private static final Logger logger = LoggerFactory.getLogger(CustomAuthenticationProvider.class); 


    public CustomAuthenticationProvider() { 
     logger.info("*** CustomAuthenticationProvider created"); 
    } 

    @Override 
    public boolean supports(Class<?> authentication) { 
     return UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication); 
    } 

    @Override 
    public Authentication authenticate(Authentication authentication) throws AuthenticationException { 

     if(authentication.getName().equals("karan") && authentication.getCredentials().equals("saman")) { 
      List<GrantedAuthority> grantedAuths = new ArrayList<>(); 
      grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER")); 
      grantedAuths.add(new SimpleGrantedAuthority("ROLE_ADMIN")); 
      return new UsernamePasswordAuthenticationToken(authentication.getName(), authentication.getCredentials(), grantedAuths); 
     } else { 
       return null; 
     } 
    } 

} 

当我尝试本地主机:8080 /登录我会收到以下错误:

Whitelabel Error Page 

This application has no explicit mapping for /error, so you are seeing this as a fallback. 
There was an unexpected error (type=Internal Server Error, status=500). 
Error resolving template "login", template might not exist or might not be accessible by any of the configured Template Resolvers 

然而,当我尝试本地主机:8080 /它会成功重定向到index.html作为我在MvcConfig.java中指定。

这里是我的login.html代码:

<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:th="http://www.thymeleaf.org" 
     xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3" 
     xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"> 

<head> 
    <meta charset="utf-8" /> 
    <title>k</title> 
</head> 

我贴我的login.html在/ src目录/主/资源/模板和/ src目录/主/ web应用/和/ src目录/主/ web应用/模板它仍然没有工作!

+0

您查看控制器配置必须拦截“登录”请求并重定向到不存在的“登录”页面,因此出现错误。这是绕过了thymeleaf视图解析器。你尝试删除该行:'registry.addViewController(“/ login”)。setViewName(“login”);'从你的MvcConfig? –

+0

@ FinbarrO'Brien是的,尝试过。实际上,我尝试添加其他类似login1的东西,并将相应的login1.html放在模板中,但我也得到了同样的错误。 – D3GAN

+0

您是将您的应用程序构建为war文件还是可执行jar? –

回答

0

好吧,所以这是pom.xml中的一个简单错误。

<!--<resources>--> 
     <!--<resource>--> 
      <!--<directory>src/main/resources</directory>--> 
      <!--<includes>--> 
       <!--<include>*</include>--> 
      <!--</includes>--> 
      <!--<filtering>true</filtering>--> 
     <!--</resource>--> 
    <!--</resources>--> 

后,我注释掉这些(你看)从POM文件,它的工作perfectly.At至少上面的代码可能是别人有用。