Как правильно переопределить метод onAuthenticationSuccess?
Как правильно переопределить метод onAuthenticationSuccess, чтобы получить логин пользователя, который только что залогинился, чтобы по этому логину вывести всю информацию о пользователе из базы (профиль пользователя)? Пока выводится пустой профиль (profile.jsp).
класс Authentication:
public class Authentication implements AuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse,
Authentication authentication)
throws IOException, ServletException {
//что здесь написать?
}
}
класс UserDaoImpl:
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private SessionFactory sessionFactory;
...
//в базе ищется введенный пользователем логин
@SuppressWarnings("unchecked")
public User getLogin(String login){
return (User) sessionFactory.getCurrentSession().createQuery(
"FROM User where login=:login").setParameter("login", login).uniqueResult();
}
}
класс UserServiceImpl:
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserDao userDao;
...
@Transactional
public User getLogin(String login){
return userDao.getLogin(login);
}
}
класс UserController:
@Controller
public class UserController {
@Autowired
private UserService userService;
...
@RequestMapping(value = "/user/profile", method = RequestMethod.GET)
public String profile(@RequestParam("findUser") String userLogin, ModelMap model, HttpSession session) {
session.setAttribute("userLogin", login);
model.put("user", (userService.getLogin(userLogin)));
return "profile";
}
}
profile.jsp
<body>
<table class="user-table">
<tr>
<th><spring:message code="label.name"/></th>
<th><spring:message code="label.surname"/></th>
<th><spring:message code="label.birthdate"/></th>
<th><spring:message code="label.address"/></th>
<th><spring:message code="label.tel"/></th>
<th><spring:message code="label.email"/></th>
<th><spring:message code="label.login"/></th>
<th><spring:message code="label.category"/></th>
<%--<th><spring:message code="label.password"/></th>--%>
<th> </th>
</tr>
<tr>
<td>${user.name}</td>
<td>${user.surname}</td>
<td><calendar:formatDate
value="${user.birthdate}" pattern="dd-MM-yyyy"/></td>
<td>${user.address}</td>
<td>${user.tel}</td>
<td>${user.email}</td>
<td>${user.login}</td>
<%--<td>${user.password}</td>--%>
<td>${user.category.name}</td>
<td><a href="${pageContext.request.contextPath}/user/edit/${user.id}">
<spring:message code="label.edit"/></a></td>
</tr>
</table>
</body>
spring-security.xml:
...
<http auto-config="true" use-expressions="true">
<headers>
<cache-control />
</headers>
<intercept-url pattern="/admin**" access="hasRole('ROLE_ADMIN')" />
<form-login login-page="/login"
default-target-url="/home"
authentication-failure-url="/login?error"
username-parameter="login"
password-parameter="password"
login-processing-url="/auth/login_check"
authentication-success-handler-ref="AuthenticationSuccessHandler"/>
<logout logout-success-url="/login?logout" delete-cookies="JSESSIONID" />
<csrf />
</http>
...
Источник: Stack Overflow на русском