【发布时间】:2016-07-19 07:38:57
【问题描述】:
我是 Spring 新手,我需要一些关于使用 Spring Security 进行身份验证的帮助。另外,如果有人可以,最好澄清一些时刻,(我会用 (#{1-..}) 标记它们)因为一开始对我来说有很多“魔法”和奇怪的事情,即使在阅读了教程和文档之后 =(.
所以,我尝试实现 AuthenticationProvider,如果我理解了 authenticate() 方法中的所有内容,我可以组织我的特定身份验证逻辑。 所以我的代码看起来像:
( (#1)如果我理解正确,Spring 会自动创建名称为 value="customAuth" 的 bean,并且无需在任何上下文文件中说明此 bean。我说的对吗?) CustomAuthenticationProvider:
@Service(value = "customAuth")
public class CustomAuthenticationProvider implements AuthenticationProvider{
@Autowired
public Storages storage;
@Override
@Transactional
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String login = authentication.getName();
String password = authentication.getCredentials().toString();
final User user = storage.uSM.findByAuthorization(login, password);
if (user==null){
return null;
} else {
return new UsernamePasswordAuthenticationToken(login, password);
}
}
@Override
public boolean supports(Class<?> authentication) {
return authentication.equals(UsernamePasswordAuthenticationToken.class);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee">
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/resources/spring-context.xml
classpath:/resources/spring-security.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/resources/spring-context.xml
classpath:/resources/spring-security.xml
</param-value>
</context-param>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/secret/page</url-pattern>
</filter-mapping>
<mvc:default-servlet-handler/>
</web-app>
spring-security.xml:
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/secret/page" access="isAuthenticated()"/>
<form-login
login-page="/sign/in"
default-target-url="/secret/page"
authentication-failure-url="/sign/in"
password-parameter="password"
username-parameter="username"
/>
</http>
<authentication-manager>
<authentication-provider ref="customAuth"/>
</authentication-manager>
</beans:beans>
下一个是带有自定义登录表单的 in.jsp 文件。正如我所读到的,此表单将默认发送到 /login(我知道我们可以通过设置 login-processing-url 来更改它)。第二个问题(#2)是关于这个“/login”的。正如我所理解的,当我们将表单发布到 /login 时,我们会将其发送到由 Spring 类创建的某个类,该类将管理它并向我们在此处设置的 AuthorizationProvider 提供必要的数据:<authentication-provider ref="customAuth"/>?还是我错了?
无论如何,它不起作用。 A尝试了很多变种,但没有一个不起作用。所以这里是我的 in.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<c:if test="${failed==1}">
<font color="red">
Authentication failed. Wrong email/password.
</font>
</c:if>
<form action="${pageContext.servletContext.contextPath}/login" method="POST">
<label> E-mail </label>
<input type="email" name="username" required><br>
<label> Password </label>
<input type="password" name="password" required><br>
<input type="submit" value="Sign in"><br>
</form>
</body>
</html>
和 In.java @Controller:
@Controller
@RequestMapping ("/sign/in")
public class In {
@RequestMapping (method = {RequestMethod.GET})
public String showForm(@RequestParam(required = false) Integer failed, ModelMap model){
model.addAttribute("failed", failed);
return "sign/in";
}
}
在这种情况下,在我发布我的表单后,它会将我重定向到 /login 并发生 404 错误,因为它不存在。
那么有人可以帮忙解决它吗?我将不胜感激任何解释、链接和想法。提前致谢。
【问题讨论】:
-
您遇到什么错误?堆栈跟踪?
-
实际上,当我尝试发布登录表单时出现 404 错误,因为它试图将我引至不存在的 /login 页面。
-
/login实际上是UsernamePasswordAuthenticationFilter存在于 Spring Security 中。你可以试试我的回答。
标签: java spring jsp spring-mvc spring-security