Commit 53370120 by lanmw

add root allow login attribute

parent c0ac0020
package com.keymobile.proxy.conf; package com.keymobile.proxy.conf;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler; import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.keymobile.proxy.api.Constants;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
@Component @Component
public class RESTAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler { public class RESTAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
@Value("${root.allowLogin:true}")
private boolean rootAllowLogin = true;
@Override @Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException { Authentication authentication) throws IOException, ServletException {
clearAuthenticationAttributes(request); clearAuthenticationAttributes(request);
String returnStatus = "ok";
//check if allow root login
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
String userNameWithIdAttached = userDetails.getUsername();
if (userNameWithIdAttached.split(":")[0].equalsIgnoreCase("root")
&& !rootAllowLogin)
returnStatus = "root not allow login";
PrintWriter writer = response.getWriter(); PrintWriter writer = response.getWriter();
writer.write("ok"); writer.write(returnStatus);
writer.flush(); writer.flush();
writer.close(); writer.close();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment