Java tutorial
/********************************************************************************** * * Copyright (c) 2017 The Sakai Foundation * * Original developers: * * Unicon based on code created by pascal alma * * Licensed under the Educational Community License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.osedu.org/licenses/ECL-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * **********************************************************************************/ package org.sakaiproject.rubrics.security; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.sakaiproject.rubrics.security.exception.JwtTokenMalformedException; import org.sakaiproject.rubrics.security.model.AuthenticatedRequestContext; import org.sakaiproject.rubrics.security.model.JwtAuthenticationToken; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider; import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.stereotype.Component; /** * Used for checking the token from the request and supply the UserDetails if the token is valid */ @Component public class JwtAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider { private final Log logger = LogFactory.getLog(this.getClass()); @Autowired private JwtTokenUtil jwtTokenUtil; @Override public boolean supports(Class<?> authentication) { return (JwtAuthenticationToken.class.isAssignableFrom(authentication)); } @Override protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException { } @Override protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException { JwtAuthenticationToken jwtAuthenticationToken = (JwtAuthenticationToken) authentication; String token = jwtAuthenticationToken.getToken(); AuthenticatedRequestContext parsedSession = jwtTokenUtil.getAuthenticatedUser(token); if (parsedSession == null) { throw new JwtTokenMalformedException(String.format("JWT token is not valid: %s", token)); } return parsedSession; } }