--- /dev/null
+package com.pentila.basic;
+
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+
+
+import org.apache.commons.codec.binary.Base64;
+
+
+import org.springframework.security.core.context.SecurityContextHolder;
+import org.springframework.security.core.userdetails.UsernameNotFoundException;
+
+
+
+public class AuthFilter implements Filter {
+
+
+
+ public void destroy() {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void doFilter(ServletRequest request, ServletResponse response,
+ FilterChain chain) throws IOException, ServletException {
+
+ loginToThread((HttpServletRequest) request);
+ try {
+
+ chain.doFilter(request, response);
+
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (ServletException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (Exception e){
+
+ }
+ logoutFromThread((HttpServletRequest) request);
+ }
+
+ private static void logoutFromThread(HttpServletRequest request) {
+
+
+ }
+
+ private static void loginToThread(HttpServletRequest request) {
+ System.out.println("debut");
+ String basicAuth = request.getHeader("login");
+ System.out.println(basicAuth);
+
+
+ if (basicAuth == null){
+ basicAuth = request.getParameter("login");
+ }
+
+ System.out.println("ECHO 1 : " + basicAuth);
+
+
+ String login = null;
+
+ if (basicAuth != null){
+
+ System.out.println("ECHO 2: " + basicAuth);
+ login = processBasicAuthToken(basicAuth);
+
+ System.out.println(login);
+ }else{
+ System.out.println("ECHO 3 : else");
+ login = request.getHeader("login");
+ System.out.println(login);
+ if (login == null) {
+ login = request.getParameter("login");
+ System.out.println("ECHO 4 : login >>> "+ login);
+ }
+
+ }
+
+
+ if ( login != null ){
+
+ System.out.println("ECHO 6 ADD TO SECURITY CONTEXT ");
+
+ FakeAuthentication fa = new FakeAuthentication(login);
+
+ SecurityContextHolder.getContext().setAuthentication(fa);
+
+ }else{
+ System.out.println("ECHO 5 excpetion >>> ");
+ throw new UsernameNotFoundException("Failed !");
+ }
+
+
+
+
+ }
+
+ private static String processBasicAuthToken(String basicAuth) {
+ if (basicAuth != null && basicAuth.startsWith("Basic ")) {
+ byte[] base64Token;
+ try {
+ base64Token = basicAuth.substring(6).getBytes("UTF-8");
+
+
+ String token = new String(Base64.decodeBase64(base64Token));
+
+ String username = "";
+
+ String password = "";
+
+ int delim = token.indexOf(":");
+
+ if (delim != -1) {
+
+ username = token.substring(0, delim);
+
+ password = token.substring(delim + 1);
+
+ System.out.println("username " + username + " / " + password);
+ return username;
+
+ }
+ } catch (UnsupportedEncodingException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ return basicAuth;
+ }
+
+
+ public void init(FilterConfig arg0) throws ServletException {
+ // TODO Auto-generated method stub
+
+ }
+
+}