点击上方 Java后端,选择 设为星标
优质文章,及时送达
写在前面
Spring Boot
创建Spring Boot应用
Spring Boot在线项目创建
http://start.spring.io/
CLI 工具
https://docs.spring.io/spring-boot/docs/current/reference/html/cli-using-the-cli.html
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
创建一个Web 应用
@SpringBootApplication
@RestController
@EnableAutoConfiguration
public class DemoApplication {
// main函数,Spring Boot程序入口
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
// 根目录映射 Get访问方式 直接返回一个字符串
@RequestMapping("/")
Map<String, String> hello() {
// 返回map会变成JSON key value方式
Map<String,String> map=new HashMap<String,String>();
map.put("content", "hello freewolf~");
return map;
}
}
{
"content": "hello freewolf~"
}
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
</dependency>
-
status - 返回状态码 0 代表正常返回,其他都是错误
-
message - 一般显示错误信息
-
result - 结果集
class JSONResult{
public static String fillResultString(Integer status, String message, Object result){
JSONObject jsonObject = new JSONObject(){{
put("status", status);
put("message", message);
put("result", result);
}};
return jsonObject.toString();
}
}
@RestController
class UserController {
// 路由映射到/users
@RequestMapping(value = "/users", produces="application/json;charset=UTF-8")
public String usersList() {
ArrayList<String> users = new ArrayList<String>(){{
add("freewolf");
add("tom");
add("jerry");
}};
return JSONResult.fillResultString(0, "", users);
}
@RequestMapping(value = "/hello", produces="application/json;charset=UTF-8")
public String hello() {
ArrayList<String> users = new ArrayList<String>(){{ add("hello"); }};
return JSONResult.fillResultString(0, "", users);
}
@RequestMapping(value = "/world", produces="application/json;charset=UTF-8")
public String world() {
ArrayList<String> users = new ArrayList<String>(){{ add("world"); }};
return JSONResult.fillResultString(0, "", users);
}
}
{
"result": [
"freewolf",
"tom",
"jerry"
],
"message": "",
"status": 0
}
curl -I http://127.0.0.1:8080/
curl -I http://127.0.0.1:8080/users
Content-Type: application/json;charset=UTF-8
Content-Type: text/plain;charset=UTF-8
@RequestMapping(value = "/users", produces="application/json;charset=UTF-8")
使用JWT保护你的 Spring Boot 应用
https://tools.ietf.org/html/rfc7519
https://jwt.io/
添加Spring Security
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.7.0</version>
</dependency>
@Configuration
@EnableWebSecurity
class WebSecurityConfig extends WebSecurityConfigurerAdapter {
// 设置 HTTP 验证规则
@Override
protected void configure(HttpSecurity http) throws Exception {
// 关闭csrf验证
http.csrf().disable()
// 对请求进行认证
.authorizeRequests()
// 所有 / 的所有请求 都放行
.antMatchers("/").permitAll()
// 所有 /login 的POST请求 都放行
.antMatchers(HttpMethod.POST, "/login").permitAll()
// 权限检查
.antMatchers("/hello").hasAuthority("AUTH_WRITE")
// 角色检查
.antMatchers("/world").hasRole("ADMIN")
// 所有请求需要身份认证
.anyRequest().authenticated()
.and()
// 添加一个过滤器 所有访问 /login 的请求交给 JWTLoginFilter 来处理 这个类处理所有的JWT相关内容
.addFilterBefore(new JWTLoginFilter("/login", authenticationManager()),
UsernamePasswordAuthenticationFilter.class)
// 添加一个过滤器验证其他请求的Token是否合法
.addFilterBefore(new JWTAuthenticationFilter(),
UsernamePasswordAuthenticationFilter.class);
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// 使用自定义身份验证组件
auth.authenticationProvider(new CustomAuthenticationProvider());
}
}
class AccountCredentials {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
class GrantedAuthorityImpl implements GrantedAuthority{
private String authority;
public GrantedAuthorityImpl(String authority) {
this.authority = authority;
}
public void setAuthority(String authority) {
this.authority = authority;
}
@Override
public String getAuthority() {
return this.authority;
}
}
class TokenAuthenticationService {
static final long EXPIRATIONTIME = 432_000_000; // 5天
static final String SECRET = "P@ssw02d"; // JWT密码
static final String TOKEN_PREFIX = "Bearer"; // Token前缀
static final String HEADER_STRING = "Authorization";// 存放Token的Header Key
// JWT生成方法
static void addAuthentication(HttpServletResponse response, String username) {
// 生成JWT
String JWT = Jwts.builder()
// 保存权限(角色)
.claim("authorities", "ROLE_ADMIN,AUTH_WRITE")
// 用户名写入标题
.setSubject(username)
// 有效期设置
.setExpiration(new Date(System.currentTimeMillis() + EXPIRATIONTIME))
// 签名设置
.signWith(SignatureAlgorithm.HS512, SECRET)
.compact();
// 将 JWT 写入 body
try {
response.setContentType("application/json");
response.setStatus(HttpServletResponse.SC_OK);
response.getOutputStream().println(JSONResult.fillResultString(0, "", JWT));
} catch (IOException e) {
e.printStackTrace();
}
}
// JWT验证方法
static Authentication getAuthentication(HttpServletRequest request) {
// 从Header中拿到token
String token = request.getHeader(HEADER_STRING);
if (token != null) {
// 解析 Token
Claims claims = Jwts.parser()
// 验签
.setSigningKey(SECRET)
// 去掉 Bearer
.parseClaimsJws(token.replace(TOKEN_PREFIX, ""))
.getBody();
// 拿用户名
String user = claims.getSubject();
// 得到 权限(角色)
List<GrantedAuthority> authorities = AuthorityUtils.commaSeparatedStringToAuthorityList((String) claims.get("authorities"));
// 返回验证令牌
return user != null ?
new UsernamePasswordAuthenticationToken(user, null, authorities) :
null;
}
return null;
}
}
// 自定义身份认证验证组件
class CustomAuthenticationProvider implements AuthenticationProvider {
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
// 获取认证的用户名 & 密码
String name = authentication.getName();
String password = authentication.getCredentials().toString();
// 认证逻辑
if (name.equals("admin") && password.equals("123456")) {
// 这里设置权限和角色
ArrayList<GrantedAuthority> authorities = new ArrayList<>();
authorities.add( new GrantedAuthorityImpl("ROLE_ADMIN") );
authorities.add( new GrantedAuthorityImpl("AUTH_WRITE") );
// 生成令牌
Authentication auth = new UsernamePasswordAuthenticationToken(name, password, authorities);
return auth;
}else {
throw new BadCredentialsException("密码错误~");
}
}
// 是否可以提供输入类型的认证服务
@Override
public boolean supports(Class<?> authentication) {
return authentication.equals(UsernamePasswordAuthenticationToken.class);
}
}
-
attemptAuthentication - 登录时需要验证时候调用
-
successfulAuthentication - 验证成功后调用
-
unsuccessfulAuthentication - 验证失败后调用,这里直接灌入500错误返回,由于同一JSON返回,HTTP就都返回200了
class JWTLoginFilter extends AbstractAuthenticationProcessingFilter {
public JWTLoginFilter(String url, AuthenticationManager authManager) {
super(new AntPathRequestMatcher(url));
setAuthenticationManager(authManager);
}
@Override
public Authentication attemptAuthentication(
HttpServletRequest req, HttpServletResponse res)
throws AuthenticationException, IOException, ServletException {
// JSON反序列化成 AccountCredentials
AccountCredentials creds = new ObjectMapper().readValue(req.getInputStream(), AccountCredentials.class);
// 返回一个验证令牌
return getAuthenticationManager().authenticate(
new UsernamePasswordAuthenticationToken(
creds.getUsername(),
creds.getPassword()
)
);
}
@Override
protected void successfulAuthentication(
HttpServletRequest req,
HttpServletResponse res, FilterChain chain,
Authentication auth) throws IOException, ServletException {
TokenAuthenticationService.addAuthentication(res, auth.getName());
}
@Override
protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException failed) throws IOException, ServletException {
response.setContentType("application/json");
response.setStatus(HttpServletResponse.SC_OK);
response.getOutputStream().println(JSONResult.fillResultString(500, "Internal Server Error!!!", JSONObject.NULL));
}
}
class JWTAuthenticationFilter extends GenericFilterBean {
@Override
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain filterChain)
throws IOException, ServletException {
Authentication authentication = TokenAuthenticationService
.getAuthentication((HttpServletRequest)request);
SecurityContextHolder.getContext()
.setAuthentication(authentication);
filterChain.doFilter(request,response);
}
}
-
先程序启动 - main函数
-
注册验证组件 - WebSecurityConfig 类 configure(AuthenticationManagerBuilder auth)方法,这里我们注册了自定义验证组件
-
设置验证规则 - WebSecurityConfig 类 configure(HttpSecurity http)方法,这里设置了各种路由访问规则
-
初始化过滤组件 - JWTLoginFilter 和 JWTAuthenticationFilter 类会初始化
curl -H "Content-Type: application/json" -X POST -d '{"username":"admin","password":"123456"}' http://127.0.0.1:8080/login
{
"result": "eyJhbGciOiJIUzUxMiJ9.eyJhdXRob3JpdGllcyI6IlJPTEVfQURNSU4sQVVUSF9XUklURSIsInN1YiI6ImFkbWluIiwiZXhwIjoxNDkzNzgyMjQwfQ.HNfV1CU2CdAnBTH682C5-KOfr2P71xr9PYLaLpDVhOw8KWWSJ0lBo0BCq4LoNwsK_Y3-W3avgbJb0jW9FNYDRQ",
"message": "",
"status": 0
}
{"alg":"HS512"}{"authorities":"ROLE_ADMIN,AUTH_WRITE","sub":"admin","exp":1493782240}ͽ]BS`pS6~hCVH%
ܬ)֝ଖoE5р
-
拿到传入JSON,解析用户名密码 - JWTLoginFilter 类 attemptAuthentication 方法
-
自定义身份认证验证组件,进行身份认证 - CustomAuthenticationProvider 类 authenticate 方法
-
盐城成功 - JWTLoginFilter 类 successfulAuthentication 方法
-
生成JWT - TokenAuthenticationService 类 addAuthentication方法
curl -H "Content-Type: application/json" -H "Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJhdXRob3JpdGllcyI6IlJPTEVfQURNSU4sQVVUSF9XUklURSIsInN1YiI6ImFkbWluIiwiZXhwIjoxNDkzNzgyMjQwfQ.HNfV1CU2CdAnBTH682C5-KOfr2P71xr9PYLaLpDVhOw8KWWSJ0lBo0BCq4LoNwsK_Y3-W3avgbJb0jW9FNYDRQ" http://127.0.0.1:8080/users
{
"result":["freewolf","tom","jerry"],
"message":"",
"status":0
}
-
接到请求进行拦截 - JWTAuthenticationFilter 中的方法
-
验证JWT - TokenAuthenticationService 类 getAuthentication 方法
-
访问Controller
代码
https://github.com/freew01f/securing-spring-boot-with-jwts
参考
https://docs.spring.io/spring-security/site/docs/current/reference/html/jc.html
http://ryanjbaxter.com/2015/01/06/securing-rest-apis-with-spring-boot/
http://www.ekiras.com/2015/01/spring-security-create-custom-authentication-filter-in-grails.html
https://auth0.com/blog/securing-spring-boot-with-jwts/
http://www.jwt.io/
作者:freewolf
juejin.im/post/5902ca705c497d005829ed6f
。同时标星(置顶)本公众号可以第一时间接受到博文推送。
本文分享自微信公众号 - Java后端(web_resource)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。
相关文章
暂无评论...