我们使用RequestHeaderAuthenticationFilteras实现预身份验证策略和PreAuthenticatedAuthenticationProvider身份验证提供程序.其中一个要求是使用以下信息将所有成功登录存储到数据库.由于用户IP地址和其他请求相关信息在UserDetailsService课堂上不可用,检索此信息并在db中存储的最佳策略是什么?
我目前以编程方式(例如,当他们通过Facebook或其他方式登录而不是使用我的登录表单)登录用户时:
SecurityContextHolder.getContext().setAuthentication(
new UsernamePasswordAuthenticationToken(user, "", authorities)
);
Run Code Online (Sandbox Code Playgroud)
我想要做的是将用户登录,就好像他们在登录表单中设置了remember-me选项一样.所以我猜我需要使用RememberMeAuthenticationToken而不是UsernamePasswordAuthenticationToken?但是,我为key构造函数的参数添加了什么?
RememberMeAuthenticationToken(String key, Object principal, Collection<? extends GrantedAuthority> authorities)
Run Code Online (Sandbox Code Playgroud)
假设我们定义了一个@Controller仅具有注释的控制器类。
在类内部,我们定义了private @Autowired HttpServletRequest request;变量。
弹簧控制器是Singleton。当我们定义HttpServletRequest为@Autowired在Web应用程序,这将是一个问题?
我从一个网站上了解到,即使@Autowired它只是为线程变量注入代理。
是真的吗 在多线程环境中,我们可以使用控制器类中的每个方法@Autowired或将其HttpServletRequest作为参数传递给正确的方法吗?
一些网站表示这是一个问题,建议将其作为参数传递,而很少有人说这将是一个问题。
我不知道哪一个是正确的。
我无法使用weblogic 12.1.3通过Jersey 2.22.1中的@Context在ContainerRequestFilter中注入HttpServletRequest.我研究了这个问题存在的几个地方,并且在很多地方我看到它在Jersey 2.4中得到修复,但我仍然看到这个问题.我的实现和代码已附加.如果我遗失任何东西,请告诉我.
AuthFilter筛选
@Provider
@Priority(Priorities.AUTHENTICATION)
public class AuthFilter implements ContainerRequestFilter {
@Context
HttpServletRequest webRequest;
@Context
HttpServletResponse webResponse;
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
final HttpSession session = webRequest.getSession();
final String userName = (String)session.getAttribute("USER_NAME");
Run Code Online (Sandbox Code Playgroud)
web.xml中
<servlet>
<servlet-name>jersey-application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value> xyz.xyz.xyz.xyz.xyz.resource</param-value>
</init-param>
</servlet>
<servlet>
<servlet-name>authentication-servlet</servlet-name>
<servlet-class>xyz.xyz.xyz.xyz.xyz.xyz.AuthenticationServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>jersey-application</servlet-name>
<url-pattern>/jaxrs/*</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
的pom.xml
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.22.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.22.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>2.22.1</version>
<exclusions>
<exclusion>
<artifactId>spring-context</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>spring-beans</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion> …Run Code Online (Sandbox Code Playgroud) 我正在研究一个java spring mvc应用程序.我已经实现了UserDetailsService这样的界面:
@Component
@Transactional
public class SecurityDAO implements UserDetailsService{
@Override
public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException {
...
}
....
}
Run Code Online (Sandbox Code Playgroud)
我需要在loadUserByUsername方法内找到用户登录URL (因为该项目有多个登录URL).实际上,我想在UserDetailsService实现中访问请求参数.
我有一个bean,我HttpServletRequest@Autowired在其中使用注释注入了一个.
当应用程序上下文是Web应用程序上下文时,此注入正常工作.对于使用Spring的JUnit测试的应用程序上下文,情况并非如此.
我该如何测试这个bean?也许我可以模拟一个http请求,但是如何在bean中注入这个模拟?
这是在Spring 3.0和Junit 4.4上
我正在使用最新的Spring Data Rest,我正在" 创建之前 " 处理该事件.我的要求是捕获提交给模型" Client " 的POST端点的HTTP Headers .但是,RepositoryEventHandler的接口不会公开它.
@Component
@RepositoryEventHandler
public class ClientEventHandler {
@Autowired
private ClientService clientService;
@HandleBeforeCreate
public void handleClientSave(Client client) {
...
...
}
}
Run Code Online (Sandbox Code Playgroud)
我们如何处理事件并捕获HTTP标头?我想访问像Spring MVC这样使用@RequestHeader HttpHeaders标头的参数.
我正在尝试为启用Spring Web安全性的应用程序创建自定义登录屏幕,我无法弄清楚如何将csrf标记传递给velocity(不,我目前无法使用JSP).
该模型看起来像这样:
@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView login(
@RequestParam(value = "error", required = false) String error,
@RequestParam(value = "logout", required = false) String logout
ModelAndView model = new ModelAndView();
if (error != null) {
model.addObject("error", "Invalid username or password!");
}
if (logout != null) {
model.addObject("msg", "You've been logged out successfully.");
}
model.setViewName("login");
return model;
}
Run Code Online (Sandbox Code Playgroud)
并且速度模板的相关部分看起来像(从jsp示例中获取和修改):
<form name='loginForm' action="/login" method='POST'>
<table>
<tr>
<td>User:</td>
<td><input type='text' name='username' value=''></td>
</tr>
<tr>
<td>Password:</td>
<td><input type='password' name='password' /></td>
</tr> …Run Code Online (Sandbox Code Playgroud) 我可以HttpServletRequest在我的RestController类似下面自动装配吗,servletRequest即使它在高度并发的环境中执行,它也会返回不同的结果。我有一个限制,我不能作为方法参数,因为我正在实现一个自动生成的接口,不会HttpServletRequest作为方法参数。
@RestController
public class MyController implements MyInterface {
@Autowired
private HttpServletRequest servletRequest;
@Override
@RequestMapping(value = "/test", produces = {"application/json"}, consumes = {"application/json"}, method = RequestMethod.POST)
public ResponseEntity<MyResponse> test(@RequestBody final MyRequest payload){
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
我已经浏览了这些 SO 问题和其他一些关于此的文章。但只是想确保当我们HttpServletRequest在控制器中自动装配时,它的Scope是Request?
Spring 3 MVC 从控制器访问 HttpRequest
如何在 Spring bean 中获取 HttpServletRequest?
注意:我确实尝试过这个,它似乎工作正常。但只是想确认即使在高度并发的环境中它也是一个万无一失的解决方案。此外,如果这是正确的方法,如果有人能解释它的工作原理,我将不胜感激。
下面是我的控制器方法定义
@Autowired
private HttpServletRequest request;
@PostMapping(path = "/abc")
public String createAbc(@RequestBody HttpServletRequest request)
throws IOException {
logger.info("Request body: "+request.getInputStream());
return "abc";
}
Run Code Online (Sandbox Code Playgroud)
我想要做的就是打印内容以请求正文。但是当我发出 POST 请求时,我看到以下错误:
类型定义错误:[简单类型,类javax.servlet.http.HttpServletRequest];嵌套异常是 com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法构造实例
javax.servlet.http.HttpServletRequest(没有创建者,如默认构造,存在):抽象类型要么需要映射到具体类型,有自定义反序列化器,要么包含其他类型信息\n 在 [来源: (PushbackInputStream); 行:1,列:2]",
我使用的是 Spring boot 2.x 版本。知道我的代码有什么问题吗?
spring ×7
spring-mvc ×7
java ×4
spring-boot ×2
httprequest ×1
jersey-2.0 ×1
junit ×1
remember-me ×1
rest ×1
scope ×1
security ×1
spring-data ×1
velocity ×1
weblogic12c ×1