我已經開發了一個Spring安全性的應用程序,並根據用戶角色(如管理員和客戶)登錄,我觀察到它記錄在app/j_spring_security_check中。我想通過身份驗證來實現具有安全性的過濾器並跟蹤所有的URL。請所說的其實是實現如何在spring security中使用過濾器並開發過濾器中的認證
回答
Security_configuration.java方式
csrfheaderfilter.java
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.web.csrf.CsrfToken;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.util.WebUtils;
public class CsrfHeaderFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class
.getName());
if (csrf != null) {
Cookie cookie = WebUtils.getCookie(request, "CSRF-TOKEN");
String token = csrf.getToken();
if (cookie == null || token != null
&& !token.equals(cookie.getValue())) {
cookie = new Cookie("CSRF-CSRF-TOKEN", token);
cookie.setPath("/main.html");
cookie.setHttpOnly(true);
cookie.setMaxAge(20);
response.addCookie(cookie);
}
}
filterChain.doFilter(request, response);
}
}
csrfrequestmatcher.java
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import org.springframework.security.web.util.matcher.RegexRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;
/**
*
* The default functionality is to skip CSRF checking for GET method. This
* functionality is lost when an explicit request matcher is provided. So, need
* to make sure that GET methods are skipped manually.
*
*/
public class CsrfRequestMatcher implements RequestMatcher {
// Always allow the HTTP GET method
private Pattern allowedMethods = Pattern.compile("^GET$");
private RegexRequestMatcher unprotectedMatcher = new RegexRequestMatcher(
"/unprotected", null);
@Override
public boolean matches(HttpServletRequest request) {
// Skip checking if request method is a GET
if (allowedMethods.matcher(request.getMethod()).matches()) {
return false;
}
// Check CSRF in all other cases.
return !unprotectedMatcher.matches(request);
}
}
感謝您的回覆。我可以知道如何在春季驗證csrf令牌 –
請附上spring-security.xml文件以供參考 –
http://www.mkyong.com/spring-security/spring-security-hello-world-example/請參考此鏈接獲取安全性xml,否則您可以使用maven來管理安全性依賴性。 – Deepanjan
- 1. Spring Security定製過濾器
- 2. Spring Security OAuth2禁用默認過濾器鏈中的BasicAuthenticationFilter
- 3. 如何在spring-security的另一個過濾器之前添加過濾器?
- 4. 過濾器如何與Spring Security中的認證提供者交互?
- 5. Spring Security CORS過濾器不起作用
- 6. Spring Security中的多個預先驗證過濾器?
- 7. 如何將證書放入X509過濾器(Spring Security)?
- 8. Spring Security addFilterAfter沒有註冊過濾器
- 9. 認證Servlet過濾器
- 10. 使用createdOn過濾器過濾結果
- 11. 在列表過濾器中使用比較器過濾器
- 12. Spring Security的401錯誤自定義驗證過濾器
- 13. 如何在javascript中使用過濾器函數過濾對象?
- 14. Spring Security多個filterChainProxy映射/過濾器,自定義過濾器Json輸出
- 15. 彈出認證過濾器不工作
- 16. 如何在ElasticSearch中使用過濾器?
- 17. Spring Security過濾器和發佈的數據
- 18. Spring攔截器/過濾器
- 19. 過濾器和過濾器
- 20. 如何使用過濾器?
- 21. 如何使用過濾器
- 22. 如何使用過濾器?
- 23. 使用自定義Grails Spring Security過濾器登錄失敗
- 24. 如何在Spring中添加過濾器(使用BlazeDS)
- 25. 在使用php時添加過濾器參數到流過濾器://過濾器
- 26. 如何在MySQL中使用這個過濾器作爲過濾器?
- 27. 如何使用1d過濾器生成2d過濾器
- 28. Spring集成XSD驗證過濾器
- 29. 添加默認過濾器顯然「禁用」過濾器原始
- 30. 使用過濾器
我有什麼實際問題毫無頭緒是。請閱讀http://stackoverflow.com/help/how-to-ask並改進您的問題。 –
春季有趣的話題我給出的解決方案的評論,你可以檢查我希望它可以幫助你。 – Deepanjan