Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. @Component
  2. public class BasicAuthAuthorizationInterceptor extends SoapHeaderInterceptor {
  3.  
  4. private static final Logger log = LoggerFactory.getLogger(BasicAuthAuthorizationInterceptor.class);
  5.  
  6. @Value("#{${accepted.username.pass1}}")
  7. private Map<String,String> authMap;
  8.  
  9. @Override
  10. public void handleMessage(Message message) throws Fault {
  11. AuthorizationPolicy policy = message.get(AuthorizationPolicy.class);
  12.  
  13. // If the policy is not set, the user did not specify credentials.
  14. // 401 is sent to the client to indicate that authentication is required.
  15. if (policy == null) {
  16. sendErrorResponse(message, HttpURLConnection.HTTP_UNAUTHORIZED);
  17. return;
  18. }
  19.  
  20. String username = policy.getUserName();
  21. String password = policy.getPassword();
  22.  
  23. // CHECK USERNAME AND PASSWORD
  24. if (!checkLogin(username,password)) {
  25.  
  26. sendErrorResponse(message, HttpURLConnection.HTTP_FORBIDDEN);
  27. }
  28. }
  29.  
  30. public boolean checkLogin(String username, String password) {
  31.  
  32.  
  33. MapUtils.debugPrint(System.out, "Map: " , authMap);
  34.  
  35. if (authMap.containsKey(username.trim()) && password.trim().equals(authMap.get(username).trim())) {
  36. return true;
  37. }
  38. return false;
  39. }
  40.  
  41. @RunWith(SpringJUnit4ClassRunner.class)
  42. @ContextConfiguration(classes = SpumConfig.class)
  43. public class BasicAuthAuthorizationInterceptorTest {
  44.  
  45. private static final Logger log = LoggerFactory.getLogger(BasicAuthAuthorizationInterceptorTest.class);
  46.  
  47. @Autowired
  48. BasicAuthAuthorizationInterceptor basicAuthAuthorizationInterceptor;
  49. @Test
  50. public void handleMessage() throws Exception {
  51. log.info(String.valueOf(basicAuthAuthorizationInterceptor.checkLogin("abc", "321")));
  52. }}
  53.  
  54. Map: =
  55. {
  56. abc = 325 java.lang.String
  57. cda = 322 java.lang.String
  58. sss = Bas3 java.lang.String
  59. } java.util.Collections$UnmodifiableMap
  60.  
  61. Map: = null
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement