- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我看过其他关于此问题的帖子,但仍然没有找到合适的答案。
我提交的表单有三个参数而不是两个。
这是我的 CustomAuthenticationProvider:
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {
private static final Logger logger = LoggerFactory.getLogger(CustomAuthenticationProvider.class);
@Override
public Authentication authenticate(Authentication auth) throws AuthenticationException {
String userName = auth.getName().trim();
String password = auth.getCredentials().toString().trim();
String companyName ;
if (userName.equals("admin") && password.equals("123456")) {
List<GrantedAuthority> grantedAuths = new ArrayList<>();
grantedAuths.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
Authentication upat = new UsernamePasswordAuthenticationToken(userName, password, grantedAuths);
logger.info("{}:{}",userName,grantedAuths);
return upat;
} else {
return null;
}
}
@Override
public boolean supports(Class<?> auth) {
return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(auth));
}
我想从登录表单获取额外参数来验证 CustomAuthenticationProvider
中的 companyName
如何从登录表单获取参数?
最佳答案
经过研究和对另一篇文章进行重大修改后,我通过注入(inject) AuthenticationDetailsSource
找到了解决方案和WebAuthenticationDetails
。所以我在这里分享解决方案:
首先为自定义过滤器创建新类来获取参数:
class ExtraParam extends WebAuthenticationDetails {
private static final long serialVersionUID = 1L;
private final String company;
public ExtraParam(HttpServletRequest request) {
super(request);
this.company = request.getParameter("company");
}
public String getCompanyName() {
return company;
}
}
然后创建用于注入(inject)的源类:
class ExtraParamSource implements AuthenticationDetailsSource<HttpServletRequest, ExtraParam> {
public ExtraParam buildDetails (HttpServletRequest context) {
return new ExtraParam(context);
}
}
然后,修改您的 spring security xml 以识别类和过滤器:
添加authentication-details-source-ref="ExtraParam"
在你的<form-login
像这样:
<form-login ... authentication-details-source-ref="ExtraParamSource"... />
不要忘记 bean :
<beans:bean id="ExtraParamSource" class="com.xxx.xxx.xxx.ExtraParamSource"/>
然后完成!就这样。要获取参数,只需使用下面的示例:
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider{
@Override
public Authentication authenticate(Authentication auth) throws AuthenticationException {
String userName = auth.getName().trim();
String password = auth.getCredentials().toString().trim();
String companyName = ((ExtraParam)auth.getDetails()).getCompanyName();
....
引用文献:
1) How to pass an additional parameter with spring security login page
关于java - 如何从登录表单调用额外参数到 CustomAuthenticationProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35215065/
我已经对此进行了研究,我认为评论(在该线程中的实际答案下方: ProviderManager.authenticate called twice for BadCredentialsException
我看过其他关于此问题的帖子,但仍然没有找到合适的答案。 我提交的表单有三个参数而不是两个。 这是我的 CustomAuthenticationProvider: @Component public c
我们使用 spring security 根据来自外部应用程序的一些用户详细信息(如 userid )对用户进行身份验证,并使用安全上下文持有者执行授权。我们正在使用 AbstractAuthenti
我正在尝试从工作 jdbc-user-service 切换到实现 AuthenticationProvider 的 customAuthenticationProvider,但是 spring 找不到
我想要获取 Spring Security 身份验证表单。这是 spring-security.xml 文件的一部分
我不清楚如何将 CustomPasswordEncoder 粘合到 Spring Boot 的身份验证过程中。我在配置中定义 Spring Boot 应该将我的 CustomAuthenticatio
我正在 Spring Security 中实现 CustomAuthenticationProvider。我创建了实现 AuthenticationProvider 的 CustomAuthentic
我是一名优秀的程序员,十分优秀!