Spring Boot应用程序Null ModelAndView返回

时间:2017-12-15 23:31:44

标签: spring spring-mvc spring-boot spring-security

我使用Java 1.8开发了一个spring boot(1.5.1.RELEASE)应用程序。我正在使用spring security和自定义用户详细信息服务。

成功验证后,我的应用程序没有显示正确的html页面/视图,它仍然保留在登录页面 - 这就是我在日志中看到的内容:

2017-12-15 23:19:43.998 DEBUG 10340 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2017-12-15 23:19:43.998 DEBUG 10340 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Successfully completed request

这是我以前多次做过的事情,但由于某种原因,这次它不起作用,我无法弄清楚原因,所以我正在寻求帮助。

这是我的安全配置类:

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

/** The Constant LOGIN_PAGE. */
private static final String LOGIN_PAGE = "/login";

/** The Constant AUTHORISED_REQUESTS_ANT_MATCHER. */
private static final String AUTHORISED_REQUESTS_ANT_MATCHER = "/**";

/** The Constant BAD_CREDENTIALS_EXCEPTION_URL. */
private static final String BAD_CREDENTIALS_EXCEPTION_URL = "/login?error";

/** The Constant ACCOUNT_EXPIRED_EXCEPTION_URL. */
private static final String ACCOUNT_EXPIRED_EXCEPTION_URL = "/login?accountexpired";

/** The Constant CREDENTIALS_EXPIRED_EXCEPTION_URL. */
private static final String CREDENTIALS_EXPIRED_EXCEPTION_URL = "/login?credentialsexpired";

/** The Constant ACCOUNT_DISABLED_EXCEPTION_URL. */
private static final String ACCOUNT_DISABLED_EXCEPTION_URL = "/login?accountdisabled";

/** The Constant ACCOUNT_LOCKED_EXCEPTION_URL. */
private static final String ACCOUNT_LOCKED_EXCEPTION_URL = "/login?accountlocked";

/** The find user by username command. */
@Autowired
private Command<FindUserByUsernameResp, FindUserByUsernameParam> findUserByUsernameCommand;

public SecurityConfig() {
    super();
}

// ===========================================
// Public Methods
// ===========================================

public Command<FindUserByUsernameResp, FindUserByUsernameParam> getFindUserByUsernameCommand() {
    return findUserByUsernameCommand;
}

public void setFindUserByUsernameCommand(Command<FindUserByUsernameResp, FindUserByUsernameParam> findUserByUsernameCommand) {
    this.findUserByUsernameCommand = findUserByUsernameCommand;
}

@Bean
public ExceptionMappingAuthenticationFailureHandler exceptionMappingAuthenticationFailureHandler() {
    Map<String, String> mappings = new HashMap<String, String>();
    mappings.put(BadCredentialsException.class.getCanonicalName(), BAD_CREDENTIALS_EXCEPTION_URL);
    mappings.put(AccountExpiredException.class.getCanonicalName(), ACCOUNT_EXPIRED_EXCEPTION_URL);
    mappings.put(CredentialsExpiredException.class.getCanonicalName(), CREDENTIALS_EXPIRED_EXCEPTION_URL);
    mappings.put(DisabledException.class.getCanonicalName(), ACCOUNT_DISABLED_EXCEPTION_URL);
    mappings.put(LockedException.class.getCanonicalName(), ACCOUNT_LOCKED_EXCEPTION_URL);

    ExceptionMappingAuthenticationFailureHandler exceptionMappingAuthenticationFailureHandler = new ExceptionMappingAuthenticationFailureHandler();
    exceptionMappingAuthenticationFailureHandler.setExceptionMappings(mappings);
    return exceptionMappingAuthenticationFailureHandler;
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(new UserDetailsServiceImpl(getFindUserByUsernameCommand()));//.passwordEncoder(new BCryptPasswordEncoder());
}

protected void configure(HttpSecurity http) throws Exception {
    http.formLogin().loginPage(LOGIN_PAGE).successForwardUrl("/")
        .and()
        .authorizeRequests()
        .antMatchers(LOGIN_PAGE).permitAll()
        .antMatchers(AUTHORISED_REQUESTS_ANT_MATCHER).authenticated();
}


}

这是我的网络配置:

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

// ===========================================
// Public Members
// ===========================================

// ===========================================
// Private Members
// ===========================================

/** The Constant ROOT_URL. */
private static final String ROOT_URL = "/";

/** The Constant ROOT_VIEW. */
private static final String ROOT_VIEW = "app/views/index";

/** The Constant LOGIN_URL. */
private static final String LOGIN_URL = "/login";

/** The Constant LOGIN_VIEW. */
private static final String LOGIN_VIEW = "login";

/*
 * (non-Javadoc)
 *
 * @see
 * org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
 * #addViewControllers(org.springframework.web.servlet.config.annotation.
 * ViewControllerRegistry)
 */
@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController(ROOT_URL).setViewName(ROOT_VIEW);
    registry.addViewController(LOGIN_URL).setViewName(LOGIN_VIEW);
}

}

我的应用程序中有两个html页面,位于templates文件夹下,它们是:

login.html(显示正常) 应用程序/视图/ index.html中

我的index.html文件永远不会显示。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

您可以尝试将ROOT_VIEW替换为/app/views/index吗?只需添加一个斜线即可。

答案 1 :(得分:0)

我发现问题是我的百万美元标记错误。我花了一段时间才发现这一点,因为我的日志中没有任何错误的证据。