Skip to content

Commit c3c3e4e

Browse files
author
zhengyangyong
committed
add authentication service based on jwt
Signed-off-by: zhengyangyong <yangyong.zheng@huawei.com>
1 parent 314afed commit c3c3e4e

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.apache.servicecomb.scaffold.user.api;
2+
3+
public interface AuthenticationService {
4+
boolean validate(String token);
5+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.apache.servicecomb.scaffold.user;
2+
3+
import org.apache.servicecomb.provider.rest.common.RestSchema;
4+
import org.apache.servicecomb.scaffold.user.api.AuthenticationService;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
9+
@RestSchema(schemaId = "authentication")
10+
@RequestMapping(path = "/")
11+
public class AuthenticationServiceImpl implements AuthenticationService {
12+
13+
private final TokenStore tokenStore;
14+
15+
@Autowired
16+
public AuthenticationServiceImpl(TokenStore tokenStore) {
17+
this.tokenStore = tokenStore;
18+
}
19+
20+
@Override
21+
@GetMapping(path = "validate")
22+
public boolean validate(String token) {
23+
return tokenStore.validate(token);
24+
}
25+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.servicecomb.scaffold.user;
18+
19+
import static io.jsonwebtoken.SignatureAlgorithm.HS512;
20+
21+
import java.time.ZonedDateTime;
22+
import java.util.Date;
23+
24+
import org.apache.commons.lang3.StringUtils;
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
27+
import org.springframework.stereotype.Component;
28+
29+
import io.jsonwebtoken.JwtException;
30+
import io.jsonwebtoken.Jwts;
31+
32+
@Component
33+
public class JwtTokenStore implements TokenStore {
34+
private static final Logger LOGGER = LoggerFactory.getLogger(JwtTokenStore.class);
35+
36+
private final String secretKey;
37+
38+
private final int secondsToExpire;
39+
40+
public JwtTokenStore() {
41+
this.secretKey = "someSecretKeyForAuthentication";
42+
this.secondsToExpire = 60 * 60 * 24;
43+
}
44+
45+
public JwtTokenStore(String secretKey, int secondsToExpire) {
46+
this.secretKey = secretKey;
47+
this.secondsToExpire = secondsToExpire;
48+
}
49+
50+
@Override
51+
public String generate(String userName) {
52+
return Jwts.builder().setSubject(userName)
53+
.setExpiration(Date.from(ZonedDateTime.now().plusSeconds(secondsToExpire).toInstant()))
54+
.signWith(HS512, secretKey).compact();
55+
}
56+
57+
@Override
58+
public boolean validate(String token) {
59+
try {
60+
return StringUtils
61+
.isNotEmpty(Jwts.parser().setSigningKey(secretKey).parseClaimsJws(token).getBody().getSubject());
62+
} catch (JwtException | IllegalArgumentException e) {
63+
LOGGER.info("validateToken token : " + token + " failed", e);
64+
}
65+
return false;
66+
}
67+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.servicecomb.scaffold.user;
19+
20+
public interface TokenStore {
21+
String generate(String userName);
22+
23+
boolean validate(String token);
24+
}

0 commit comments

Comments
 (0)