【问题标题】:Spring Boot Application Null ModelAndView returnedSpring Boot Application Null ModelAndView 返回
【发布时间】:2017-12-15 23:31:44
【问题描述】:

我使用 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 页面,位于模板文件夹下,它们是:

login.html(显示正常) app/views/index.html

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

有什么想法吗?

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我发现问题是我的百里香标记中的错误。我花了一段时间才发现这一点,因为没有证据表明我的日志中有错误。

      【讨论】:

        猜你喜欢
        • 2020-05-24
        • 2019-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-13
        • 2015-10-31
        • 2019-03-14
        • 2020-02-25
        相关资源
        最近更新 更多