[英]Spring security how to properly apply filter to get param from URL
在spring security中,我需要使用用戶嘗試訪問的站點的param進行自定義身份驗證,例如:
用戶嘗試訪問:
myapp.com/res?param=value
在身份驗證過程中,我需要從param中獲取此value
,如何獲得此值?
我也有自定義UserDetailsService
我需要此param
。
我正在嘗試這樣的事情:
public class MyFilter extends UsernamePasswordAuthenticationFilter {
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) arg0;
System.out.println("\n\n"+request.getRequestURI().substring(request.getContextPath().length()) +"\n\n");
super.doFilter(arg0, arg1, arg2);
}
}
我曾提出:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.addFilterAfter(
new MyFilter(), BasicAuthenticationFilter.class);
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.and()
.httpBasic();
}
該過濾器使用我的參數打印出URL,但是如何在自定義UserDetailsService
呢? 另外,我不確定此篩選器是否正確。
我在spring 5.0.2中使用spring-security 5.0.0
謝謝,
如何在我的自定義
UserDetailsService
提供此信息?
通過使用Spring
內置的請求處理程序。
@RestController
public class UserDetailService {
@RequestMapping(value = "/res", method = RequestMethod.GET)
public void getRes(
@RequestParam(required = false, defaultValue = "value", value="param") final String param) {
// param contains the value of the parameter in the URL
}
}
您應該僅使用filter
來驗證請求,並在需要時阻止它們。
所有業務邏輯都應在@Controller
和@Service
類中實現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.