Tôi là người mới sử dụng Spring Security 3. Tôi đang sử dụng vai trò để người dùng đăng nhập.Cách thêm bộ lọc tùy chỉnh sau khi người dùng ủy quyền trong ứng dụng mùa xuân
Tôi muốn thêm một số giá trị phiên sau khi người dùng được ủy quyền vào ứng dụng. Có lẽ tôi cần một số bộ lọc để nó chuyển hướng đến phương pháp của tôi mà thêm một số giá trị phiên. Tôi đã cấu hình tệp security.xml của mình nhưng tôi không chắc liệu mình có đang làm đúng hay không. Bất kỳ ví dụ nào theo hướng đó sẽ hữu ích. Tôi nên sử dụng lớp bộ lọc nào? Tôi nên cấu hình tập tin security.xml như thế nào?
<custom-filter ref="authenticationFilter" after="FORM_LOGIN_FILTER "/>
<beans:bean id="authenticationFilter" class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
<beans:property name="filterProcessesUrl" value="/j_spring_security_check" />
<beans:property name="authenticationManager" ref="authenticationManager" />
<beans:property name="authenticationSuccessHandler" ref="successHandler" />
</beans:bean>
<beans:bean id="successHandler" class="org.dfci.sparks.datarequest.security.CustomAuthorizationFilter"/>
Phương pháp lớp bộ lọc của tôi tôi cần thêm một số giá trị phiên.
public class CustomAuthorizationFilter implements AuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication authentication)
throws IOException, ServletException {
Set<String> roles = AuthorityUtils.authorityListToSet(authentication
.getAuthorities());
if (roles.contains("ROLE_USER")) {
request.getSession().setAttribute("myVale", "myvalue");
}
}
}
Sửa Mã
tôi đã sửa đổi tập tin security.xml tôi và tập tin lớp
<custom-filter ref="authenticationFilter" after="FORM_LOGIN_FILTER "/>
public class CustomAuthorizationFilter extends GenericFilterBean {
/*
* ServletRequestAttributes attr = (ServletRequestAttributes)
* RequestContextHolder.currentRequestAttributes(); HttpSession
* session=attr.getRequest().getSession(true);
*/
@Autowired
private UserService userService;
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
try {
chain.doFilter(request, response);
HttpServletRequest req = (HttpServletRequest) request;
HttpSession session = req.getSession(true);
Authentication authentication = SecurityContextHolder
.getContext().getAuthentication();
Set<String> roles = AuthorityUtils
.authorityListToSet(authentication.getAuthorities());
User user = null;
if (true) {
session.setAttribute("Flag", "Y");
}
}
} catch (IOException ex) {
throw ex;
}
}
}
nào gọi mỗi URL. Có cách nào khác để gọi phương thức lọc chỉ một lần khi người dùng được xác thực không?
Sự cố với mã ở trên là gì? Bạn không nhận được thuộc tính myValue phiên bên trong url thành công khi đăng nhập? –
@SunilChavan: Nó gọi bộ lọc này trước khi người dùng được xác thực. – Raje
Bạn có thể nói như thế nào? Xem (API) [http://static.springsource.org/spring-security/site/docs/3.0.x/apidocs/org/springframework/security/web/authentication/AuthenticationSuccessHandler.html] chi tiết. Nó nói rõ ràng sau khi xác thực thành công nó gọi phương thức onAuthenticationSuccess –