Skip to content

Instantly share code, notes, and snippets.

@BohdanLevchenko
Last active July 5, 2017 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BohdanLevchenko/18c86cb5dec42dfc17e04dcd7d1563be to your computer and use it in GitHub Desktop.
Save BohdanLevchenko/18c86cb5dec42dfc17e04dcd7d1563be to your computer and use it in GitHub Desktop.
package com.stackoverflow.so44928052;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.MethodParameter;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.WebArgumentResolver;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import io.jsonwebtoken.*;
/**
* Set your key in application.properties:
* security.jwt.key=JWTKEY
*/
@SpringBootApplication
public class So44928052Application {
public static void main(String[] args) {
SpringApplication.run(So44928052Application.class, args);
}
@Bean
CommandLineRunner runner(@Value("security.jwt.key") String key) {
return args -> {
String compactJws = Jwts.builder().setSubject("Joe").signWith(SignatureAlgorithm.HS512, key).compact();
System.out.println(compactJws); // prints token just for demo purposes
};
}
@Bean
JwtParser jwtParser(@Value("security.jwt.key") String key) {
return Jwts.parser().setSigningKey(key);
}
@Configuration
public static class Mvc extends WebMvcConfigurationSupport {
@Autowired
JwtParser jwtParser;
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(new JwtUserHandlerMethodArgumentResolver(jwtParser));
}
}
public static class JwtUserHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {
private final JwtParser jwtParser;
JwtUserHandlerMethodArgumentResolver(JwtParser jwtParser) { this.jwtParser = jwtParser; }
@Override
public boolean supportsParameter(MethodParameter methodParameter) {
return methodParameter.getParameterAnnotation(Autowired.class) != null
&& methodParameter.getParameterType().equals(String.class);
}
@Override
public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
if (this.supportsParameter(methodParameter)) {
final String authorization = webRequest.getHeader("Authorization");
if (!StringUtils.isEmpty(authorization)) {
return this.jwtParser.parseClaimsJws(authorization.substring(7)).getBody().getSubject();
} else {
return null;
}
}
return WebArgumentResolver.UNRESOLVED;
}
}
@RestController
public static class GreetingController {
private static final String template = "Hello, %s!";
@RequestMapping(value = {"/greeting"}, method = RequestMethod.GET)
public String index(@Autowired String username) {
return String.format(template, username);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment