Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Component
- public class BasicAuthAuthorizationInterceptor extends SoapHeaderInterceptor {
- private static final Logger log = LoggerFactory.getLogger(BasicAuthAuthorizationInterceptor.class);
- @Value("#{${accepted.username.pass1}}")
- private Map<String,String> authMap;
- @Override
- public void handleMessage(Message message) throws Fault {
- AuthorizationPolicy policy = message.get(AuthorizationPolicy.class);
- // If the policy is not set, the user did not specify credentials.
- // 401 is sent to the client to indicate that authentication is required.
- if (policy == null) {
- sendErrorResponse(message, HttpURLConnection.HTTP_UNAUTHORIZED);
- return;
- }
- String username = policy.getUserName();
- String password = policy.getPassword();
- // CHECK USERNAME AND PASSWORD
- if (!checkLogin(username,password)) {
- sendErrorResponse(message, HttpURLConnection.HTTP_FORBIDDEN);
- }
- }
- public boolean checkLogin(String username, String password) {
- MapUtils.debugPrint(System.out, "Map: " , authMap);
- if (authMap.containsKey(username.trim()) && password.trim().equals(authMap.get(username).trim())) {
- return true;
- }
- return false;
- }
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(classes = SpumConfig.class)
- public class BasicAuthAuthorizationInterceptorTest {
- private static final Logger log = LoggerFactory.getLogger(BasicAuthAuthorizationInterceptorTest.class);
- @Autowired
- BasicAuthAuthorizationInterceptor basicAuthAuthorizationInterceptor;
- @Test
- public void handleMessage() throws Exception {
- log.info(String.valueOf(basicAuthAuthorizationInterceptor.checkLogin("abc", "321")));
- }}
- Map: =
- {
- abc = 325 java.lang.String
- cda = 322 java.lang.String
- sss = Bas3 java.lang.String
- } java.util.Collections$UnmodifiableMap
- Map: = null
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement