使用springSecurity验证登录密码基于SpringMVC模式,在配置文件中默认返回一个页面,但是我们有时候因需求原因需要自定义返回某个页面而不是配置文件中的默认页面,所以我们需要重写AuthenticationSuccessHandler
这个类
请看例子,具体逻辑根据需求进行操作
import org.apache.commons.lang.StringUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class LoginAuthenticationSuccessHandle implements AuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
String path = request.getContextPath();
String roles=request.getParameter("haikangs");
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
if(!StringUtils.isEmpty(roles)){
if ("haikangs".equals(roles)){
response.sendRedirect(basePath);
}
}else{
response.sendRedirect(basePath+"index.html");
}
}
}
我这边的实现是携带参数进行判断需要返回的是哪个页面
重新该类后需要在配置文件进行注册,原本在该处是默认返回某一页面的,那么现在可以注释掉了
加入
<bean id="successHandler"
class="com.raymon.manage.security.LoginAuthenticationSuccessHandle" />
LoginAuthenticationSuccessHandle 是重写的类
到此处就完结了,可以大佬去试试吧。如果本文有误导的地方还请指出,谢谢