Tag: spring 3

Spring 3安全性j_spring_security_check

我正在努力学习Spring安全性如何工作,所以我下载了一些示例项目,然后我尝试将该解决方案实现到我的项目中。 但是当我尝试登录时,我收到404错误,在地址栏中我有http://localhost:8080/fit/j_spring_security_check 。 我试着在这里查看类似的问题,但我无法意识到,如何将它应用到我的项目中。 如果有经验丰富的人可以帮助我,我会非常感激。 我的app结构如下所示: applicationContext.xml中: 的applicationContext-web.xml中: 的applicationContext-security.xml文件:

升级到springframework.scheduling.concurrent?

从Spring 3.0开始,不推荐使用ScheduledTimerTask,我无法理解如何升级到org.springframework.scheduling.concurrent。 OnlineTimerTask扩展java.util.TimerTask的位置。 这是一项简单的任务,每分钟都会向发布者发布一条消息。 我检查了文档,但没有..我无法理解从并发包使用哪种方式,哪种方式最适合。 此外,我想在Java中将此xml转换为@Bean。 编辑:所以我尝试用@Bean和@Configuration实现xml,这就是我得到的。 @Configuration public class ContextConfiguration { @Bean public ScheduledExecutorFactoryBean scheduledExecutorFactoryBean() { ScheduledExecutorFactoryBean scheduledFactoryBean = new ScheduledExecutorFactoryBean(); scheduledFactoryBean.setScheduledExecutorTasks(new ScheduledExecutorTask[] {onlineTimeSchedule()}); return scheduledFactoryBean; } @Bean public ScheduledExecutorTask onlineTimeSchedule() { ScheduledExecutorTask scheduledTask = new ScheduledExecutorTask(); scheduledTask.setDelay(1000); scheduledTask.setPeriod(60000); scheduledTask.setRunnable(new OnlineTimerTask()); return scheduledTask; } } 上面的代码是否正确替换xml? 在我的情况下,setScheduledExecutorTasks会正常工作吗? 我的意思是,如果不止一次调用onlineTimeSchedule(),那么引用同一个bean实例会在这里工作吗? scheduledFactoryBean.setScheduledExecutorTasks(new ScheduledExecutorTask[] {onlineTimeSchedule()});

Spring 3安全性:未调用AccessDeniedHandler

我有一个spring 3应用程序,其配置如下。 当任何用户尝试访问某个页面并且他/她未登录时,我会收到一个带有丑陋堆栈跟踪的Access is Deniedexception。 如何处理此exception并且不允许它转储堆栈跟踪。 我实现了自己的访问被拒绝处理程序但不会被调用。 根据所请求资源的类型,我想显示自定义错误消息或页面。 这是我的弹簧配置。 如何让Spring调用我的访问被拒绝处理程序。 这是我的弹簧配置 下面给出了处理此exception的自定义类 public class AccessDeniedExceptionHandler implements AccessDeniedHandler { private String errorPage; @Override public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException arg2) throws IOException, ServletException { response.sendRedirect(errorPage); } public void setErrorPage(String errorPage) { if ((errorPage != null) && !errorPage.startsWith(“/”)) { throw new IllegalArgumentException(“errorPage must begin with ‘/'”); […]

升级到Spring 3.2后出现HttpMediaTypeNotAcceptableException

将我的Spring MVC应用程序升级到Spring 3.2后,在访问我的一些URL时遇到以下exception: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation at org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:203) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE] at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:272) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE] at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:212) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE] at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:55) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE] at org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:297) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE] at org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1091) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE] at org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1076) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:896) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915) [spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE] (…) 此exception导致HTTP 406无法接受。 我设法用我无法访问的URL创建一个简化的控制器: @RequestMapping(value = “/resources/foo.js”, produces = “text/javascript”) @ResponseBody public String foo() throws Exception […]

使用FileSystemResource强制文件下载文件时如何设置’Content-Disposition’和’Filename’?

使用Spring 3 FileSystemResource设置Content-Disposition=attachment和filename=xyz.zip的最合适,最标准的方法是什么? 该动作如下: @ResponseBody @RequestMapping(value = “/action/{abcd}/{efgh}”, method = RequestMethod.GET, produces = “application/zip”) @PreAuthorize(“@authorizationService.authorizeMethod()”) public FileSystemResource doAction(@PathVariable String abcd, @PathVariable String efgh) { File zipFile = service.getFile(abcd, efgh); return new FileSystemResource(zipFile); } 虽然该文件是一个zip文件,因此浏览器总是下载该文件,但我想明确提到该文件作为附件,并提供与文件实际名称无关的文件名。 这个问题可能有解决方法,但我想知道正确的Spring和FileSystemResource方法来实现这个目标。 PS此处使用的文件是临时文件,在JVM存在时标记为删除。

Spring 3.0dependency injection的最小JAR

与早期Spring版本的这个问题类似,应用程序仅使用Spring 3.0dependency injection所需的最小依赖项是什么? 应用程序上下文仅由XML配置。 Spring依赖于日志框架,所以假设我已经包含这些JAR用于日志记录: JCL-过slf4j.jar 的logback-classic.jar 的logback-core.jar添加 SLF4J-api.jar文件

当url模式是路径时,无法让Spring MVC调度程序正常工作

我有一个Web应用程序,我们目前正在为REST服务应用spring MVC。 我们希望我们的rest服务显示在${contextPath}/rest/** ,但是当我设置它时,我们得到: 在DispatcherServlet中找不到具有URI [/ myapp / rest / testSvc / message]的HTTP请求的映射,其名称为“Spring MVC Dispatcher Servlet” 我的web.xml有: Spring MVC Dispatcher Servlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation /WEB-INF/spring/servlet-context.xml 1 Spring MVC Dispatcher Servlet /rest servlet-context.xml ,这很好,并且在启动时注册服务。 我的控制器如下所示: @Controller @RequestMapping(value = “/rest/testService”) public class TestREST { @RequestMapping(value=”message”, method=RequestMethod.GET) public @ResponseBody String getMessage() { return “REST working”; } 如果我将web.xml的url-pattern更改为* .rest,并将我的请求映射message给message.rest则可以正常工作。

在Spring 3中创建单一测试

我开始测试应用程序,我想创建几个测试来学习Spring中的Mockito。 我一直在阅读一些信息,但我有一些普遍的疑问,我想问。 我见过Mockito测试,他们用类: @RunWith(MockitoJUnitRunner.class)注释类的测试,而在Spring文档中使用@RunWith(SpringJUnit4ClassRunner.class) 。 我不知道它们之间有什么区别,我应该将哪一个用于测试使用Mockito的Spring应用程序。 由于我还没有看到任何有测试的实际应用程序,我想知道开发人员会做的典型测试。 例如,在用户的典型CRUD应用程序(用户可以创建,更新……)中,任何人都可以进行常规测试。 谢谢。

如果找不到,@ PathVariable可以返回null吗?

如果路径变量不在url中,是否可以使@PathVariable返回null? 否则我需要做两个处理程序。 一个用于/simple ,另一个用于/simple/{game} ,但两者都做同样的事情,如果没有定义游戏我从列表中选择第一个然而如果有游戏参数定义然后我使用它。 @RequestMapping(value = {“/simple”, “/simple/{game}”}, method = RequestMethod.GET) public ModelAndView gameHandler(@PathVariable(“example”) String example, HttpServletRequest request) { 这是我在尝试打开页面/simple : 引起:java.lang.IllegalStateException:在@RequestMapping中找不到@PathVariable [example]

如何在spring中为xml中定义的bean选择构造函数?

我正在使用Spring 3.1.0。 我试图了解spring读取其xml文件的方式。 我试图理解spring如何处理bean定义中的模糊条件。 例如 我有Alarm.java package com.anshbansal.alarm2; public class Alarm { private String type; private int volume; private int hour; public Alarm() { } public Alarm(String type, int volume, int hour) { this.type = type; this.volume = volume; this.hour = hour; } public Alarm(String type, int volume) { this.type = type; this.volume = volume; […]