Ana*_*sra 6 java spring spring-mvc spring-security spring-java-config
我在春天写了一个小应用程序来学习java配置,因为我已经被同行唠叨了一段时间来升级我们的应用程序;-),一个简单的待办事项列表应用程序,它具有安全性和web mvc配置,JPA用于持久性,所有通过java配置.我在尝试运行应用程序时遇到了问题.scurity配置和JPA等工作正常但在成功拦截受保护的URL后我得到一个空视图
主Web应用程序初始化程序类扩展 AbstractAnnotationConfigDispatcherServletInitializer
public class WiggleWebApplicationInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { WiggleApplicationConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { WiggleWebAppConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
@Override
protected void registerDispatcherServlet(ServletContext servletContext) {
super.registerDispatcherServlet(servletContext);
servletContext.addListener(new HttpSessionEventPublisher());
}
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding("UTF-8");
characterEncodingFilter.setForceEncoding(true);
return new Filter[] { characterEncodingFilter };
}
}
Run Code Online (Sandbox Code Playgroud)
在WiggleApplicationConfig进口安全,JPA和社会
@Configuration
@ComponentScan(basePackages = { "wiggle.app.services.*" })
@Import({ WigglePersistenceConfig.class, WiggleSecurityConfig.class,
WiggleSocialConfig.class })
public class WiggleApplicationConfig {
@Bean
public DateFormat dateFormat() {
return new SimpleDateFormat("dd-MM-yyyy");
}
}
Run Code Online (Sandbox Code Playgroud)
然后,Web配置添加默认处理程序等
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "wiggle.app.controllers.*" })
public class WiggleWebAppConfig extends WebMvcConfigurerAdapter {
private static final String VIEW_RESOLVER_PREFIX = "/WEB-INF/jsp/";
private static final String VIEW_RESOLVER_SUFFIX = ".jsp";
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations(
"/static/");
}
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Bean
public SimpleMappingExceptionResolver exceptionResolver() {
SimpleMappingExceptionResolver exceptionResolver = new SimpleMappingExceptionResolver();
Properties exceptionMappings = new Properties();
exceptionMappings.put("java.lang.Exception", "error/error");
exceptionMappings.put("java.lang.RuntimeException", "error/error");
exceptionResolver.setExceptionMappings(exceptionMappings);
Properties statusCodes = new Properties();
statusCodes.put("error/404", "404");
statusCodes.put("error/error", "500");
exceptionResolver.setStatusCodes(statusCodes);
return exceptionResolver;
}
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix(VIEW_RESOLVER_PREFIX);
viewResolver.setSuffix(VIEW_RESOLVER_SUFFIX);
return viewResolver;
}
}
Run Code Online (Sandbox Code Playgroud)
所有这些都存在于包中wiggle.app.config,通过我的配置/**受到保护并且应该重定向到/ login,这对所有人开放,安全过滤器链确实可以正常工作,我看到Access Denied之后有重定向到/ wiggle/login如何当我访问主页时,我得到了404以后的日志条目http://localhost:8080/wiggle/
Previously Authenticated: org.springframework.security.authentication.AnonymousAuthenticationToken@6faeba70: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@fffbcba8: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: 8A7C29831E56336A6FDF1A0E19200E70; Granted Authorities: ROLE_ANONYMOUS
Voter: org.springframework.security.web.access.expression.WebExpressionVoter@c01ac1b, returned: 1
Authorization successful
RunAsManager did not change Authentication object
/login reached end of additional filter chain; proceeding with original chain
DispatcherServlet with name 'dispatcher' processing GET request for [/wiggle/login]
Looking up handler method for path /login
Did not find handler method for [/login]
Matching patterns for request [/login] are [/**]
URI Template variables for request [/login] are {}
Mapping [/login] to HandlerExecutionChain with handler [org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler@688a42b5] and 1 interceptor
Last-Modified value for [/wiggle/login] is: -1
SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.
Null ModelAndView returned to DispatcherServlet with name 'dispatcher': assuming HandlerAdapter completed request handling
Successfully completed request
Chain processed normally
SecurityContextHolder now cleared, as request processing completed
Run Code Online (Sandbox Code Playgroud)
我通常会将以下内容放在XML中来处理映射
<beans:bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<!-- Enables annotated POJO @Controllers -->
<beans:bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
Run Code Online (Sandbox Code Playgroud)
和
<!-- Scans within the base package of the application for @Components to configure as beans -->
<context:component-scan base-package="com.code.controller" />
Run Code Online (Sandbox Code Playgroud)
我无法找到我缺少的东西,以启用Java配置的类似行为.
事实证明,我错过了关于此配置的重要文档,Spring Framework 文档中的16.16.8 mvc:default-servlet-handler节
\n\n\n\n\n覆盖“/”Servlet 映射的注意事项是,必须按名称而不是按路径检索默认 Servlet 的 RequestDispatcher。DefaultServletHttpRequestHandler 将尝试在启动时自动检测容器的默认 Servlet,使用大多数主要 Servlet 容器(包括 Tomcat、Jetty、GlassFish、JBoss、Resin、WebLogic 和 WebSphere)的已知名称列表。如果默认 Servlet 已使用不同的名称进行自定义配置,或者正在使用默认 Servlet 名称未知的不同 Servlet 容器,则必须显式提供默认 Servlet\xe2\x80\x99s 名称,如下例所示:
\n
@Configuration\n@EnableWebMvc\npublic class WebConfig extends WebMvcConfigurerAdapter {\n\n @Override\n public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {\n configurer.enable("myCustomDefaultServlet");\n }\nRun Code Online (Sandbox Code Playgroud)\n\n}
\n\n因此我改变了这个
\n\n@Override\npublic void configureDefaultServletHandling(\n DefaultServletHandlerConfigurer configurer) {\n configurer.enable("wiggleServlet");\n}\nRun Code Online (Sandbox Code Playgroud)\n\n还有另一处配置错误
\n\n@Configuration\n@EnableWebMvc\n@ComponentScan(basePackages = { "wiggle.app.controllers.*" })\npublic class WiggleWebAppConfig extends WebMvcConfigurerAdapter {\nRun Code Online (Sandbox Code Playgroud)\n\n应该
\n\n@Configuration\n@EnableWebMvc\n@ComponentScan(basePackages = { "wiggle.app.controllers" })\npublic class WiggleWebAppConfig extends WebMvcConfigurerAdapter {\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
5915 次 |
| 最近记录: |