Skip to content

Commit 90d430a

Browse files
author
YuI
committed
feat: add shiro and jwt.
1 parent 5b4cb5a commit 90d430a

9 files changed

Lines changed: 501 additions & 0 deletions

File tree

‎pom.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@
118118
<wiremock.version>2.18.0</wiremock.version>
119119
<zookeeper.version>3.5.6</zookeeper.version>
120120
<zkclient.version>0.10</zkclient.version>
121+
<shiro.version>1.7.0</shiro.version>
122+
<jwt.version>3.12.0</jwt.version>
121123
<!--maven plugin version-->
122124
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
123125
<apache-rat-plugin.version>0.12</apache-rat-plugin.version>

‎soul-admin/pom.xml‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@
6969
<optional>true</optional>
7070
</dependency>
7171

72+
<dependency>
73+
<groupId>org.apache.shiro</groupId>
74+
<artifactId>shiro-spring-boot-web-starter</artifactId>
75+
<version>${shiro.version}</version>
76+
</dependency>
77+
78+
<dependency>
79+
<groupId>com.auth0</groupId>
80+
<artifactId>java-jwt</artifactId>
81+
<version>${jwt.version}</version>
82+
</dependency>
83+
7284
<dependency>
7385
<groupId>org.projectreactor</groupId>
7486
<artifactId>reactor-spring</artifactId>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.dromara.soul.admin.config;
19+
20+
import lombok.Data;
21+
import org.springframework.boot.context.properties.ConfigurationProperties;
22+
import org.springframework.stereotype.Component;
23+
24+
/**
25+
* Jwt Properties.
26+
*
27+
* @author YuI
28+
**/
29+
@Data
30+
@Component
31+
@ConfigurationProperties(prefix = "soul.jwt")
32+
public class JwtProperties {
33+
34+
private String key;
35+
36+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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.dromara.soul.admin.shiro.bean;
19+
20+
import lombok.extern.slf4j.Slf4j;
21+
import org.apache.commons.lang3.StringUtils;
22+
import org.apache.shiro.authc.AuthenticationException;
23+
import org.apache.shiro.subject.Subject;
24+
import org.apache.shiro.web.filter.AccessControlFilter;
25+
26+
import javax.servlet.ServletRequest;
27+
import javax.servlet.ServletResponse;
28+
import javax.servlet.http.HttpServletRequest;
29+
import javax.servlet.http.HttpServletResponse;
30+
import java.io.IOException;
31+
32+
/**
33+
* custom Stateless AccessControlFilter.
34+
*
35+
* @author YuI
36+
**/
37+
@Slf4j
38+
public class StatelessAuthFilter extends AccessControlFilter {
39+
40+
private static final String HEAD_TOKEN = "X-Access-Token";
41+
42+
@Override
43+
protected boolean isAccessAllowed(final ServletRequest servletRequest, final ServletResponse servletResponse,
44+
final Object o) throws Exception {
45+
return false;
46+
}
47+
48+
@Override
49+
protected boolean onAccessDenied(final ServletRequest servletRequest, final ServletResponse servletResponse)
50+
throws Exception {
51+
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
52+
String tokenValue = httpServletRequest.getHeader(HEAD_TOKEN);
53+
54+
if (StringUtils.isEmpty(tokenValue)) {
55+
onLoginFail(servletResponse);
56+
return false;
57+
}
58+
59+
StatelessToken token = new StatelessToken();
60+
token.setToken(tokenValue);
61+
62+
Subject subject = getSubject(servletRequest, servletResponse);
63+
try {
64+
subject.login(token);
65+
} catch (AuthenticationException e) {
66+
log.warn("token is warning, token : {}", token, e);
67+
onLoginFail(servletResponse);
68+
return false;
69+
}
70+
71+
return true;
72+
}
73+
74+
/**
75+
* union response same result form exception.
76+
* todo: will change to global exception intercept.
77+
*
78+
* @param response {@link ServletResponse}
79+
*/
80+
private void onLoginFail(final ServletResponse response) throws IOException {
81+
HttpServletResponse httpResponse = (HttpServletResponse) response;
82+
httpResponse.setHeader("Content-type", "text/html;charset=UTF-8");
83+
httpResponse.setCharacterEncoding("utf-8");
84+
wrapCorsResponse(httpResponse);
85+
httpResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
86+
httpResponse.getWriter().write("token失效");
87+
}
88+
89+
/**
90+
* add cors.
91+
*
92+
* @param response {@link ServletResponse}
93+
*/
94+
private void wrapCorsResponse(final HttpServletResponse response) {
95+
response.addHeader("Access-Control-Allow-Origin", "*");
96+
response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
97+
response.addHeader("Access-Control-Allow-Headers", "Content-Type");
98+
response.addHeader("Access-Control-Max-Age", "1800");
99+
}
100+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.dromara.soul.admin.shiro.bean;
19+
20+
import lombok.Data;
21+
import org.apache.shiro.authc.AuthenticationToken;
22+
23+
/**
24+
* custom Stateless AuthenticationToken.
25+
*
26+
* @author YuI
27+
**/
28+
@Data
29+
public class StatelessToken implements AuthenticationToken {
30+
31+
private String userName;
32+
33+
private String token;
34+
35+
@Override
36+
public Object getPrincipal() {
37+
return userName;
38+
}
39+
40+
@Override
41+
public Object getCredentials() {
42+
return token;
43+
}
44+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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.dromara.soul.admin.shiro.config;
19+
20+
import org.apache.shiro.realm.AuthorizingRealm;
21+
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
22+
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
23+
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
24+
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
25+
import org.dromara.soul.admin.shiro.bean.StatelessAuthFilter;
26+
import org.springframework.beans.factory.annotation.Qualifier;
27+
import org.springframework.context.annotation.Bean;
28+
import org.springframework.context.annotation.Configuration;
29+
30+
import javax.servlet.Filter;
31+
import java.util.LinkedHashMap;
32+
import java.util.LinkedList;
33+
import java.util.List;
34+
import java.util.Map;
35+
36+
/**
37+
* shiro configuration.
38+
*
39+
* @author YuI
40+
**/
41+
@Configuration
42+
public class ShiroConfiguration {
43+
44+
/**
45+
* white list.
46+
*/
47+
private static final List<String> WHITE_LIST;
48+
49+
static {
50+
WHITE_LIST = new LinkedList<>();
51+
WHITE_LIST.add("/index");
52+
WHITE_LIST.add("/platform/login");
53+
}
54+
55+
/**
56+
* generate AuthorizingRealm bean.
57+
*
58+
* @return {@link AuthorizingRealm}
59+
*/
60+
@Bean("shiroRealm")
61+
public AuthorizingRealm shiroRealm() {
62+
return new ShiroRealm();
63+
}
64+
65+
/**
66+
* generate WebSecurityManager.
67+
*
68+
* @param shiroRealm shiro's realm
69+
* @return {@link DefaultWebSecurityManager}
70+
*/
71+
@Bean("shiroSecurityManager")
72+
public DefaultWebSecurityManager securityManager(@Qualifier("shiroRealm") final AuthorizingRealm shiroRealm) {
73+
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
74+
securityManager.setRealm(shiroRealm);
75+
return securityManager;
76+
}
77+
78+
/**
79+
* ShiroFilterFactoryBean.
80+
*
81+
* @param securityManager {@link DefaultWebSecurityManager}
82+
* @return {@link ShiroFilterFactoryBean}
83+
*/
84+
@Bean
85+
public ShiroFilterFactoryBean shiroFilterFactoryBean(
86+
@Qualifier("shiroSecurityManager") final DefaultWebSecurityManager securityManager) {
87+
ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
88+
factoryBean.setSecurityManager(securityManager);
89+
Map<String, Filter> filterMap = new LinkedHashMap<>();
90+
filterMap.put("statelessAuth", new StatelessAuthFilter());
91+
factoryBean.setFilters(filterMap);
92+
93+
Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
94+
95+
for (String s : WHITE_LIST) {
96+
filterChainDefinitionMap.put(s, "anon");
97+
}
98+
99+
filterChainDefinitionMap.put("/**", "statelessAuth");
100+
factoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
101+
return factoryBean;
102+
}
103+
104+
/**
105+
* AuthorizationAttributeSourceAdvisor.
106+
*
107+
* @param securityManager {@link DefaultWebSecurityManager}
108+
* @return {@link AuthorizationAttributeSourceAdvisor}
109+
*/
110+
@Bean
111+
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(
112+
@Qualifier("shiroSecurityManager") final DefaultWebSecurityManager securityManager) {
113+
AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
114+
authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
115+
return authorizationAttributeSourceAdvisor;
116+
}
117+
118+
/**
119+
* shiro's lifecycle in spring.
120+
*
121+
* @return {@link LifecycleBeanPostProcessor}
122+
*/
123+
@Bean
124+
public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
125+
return new LifecycleBeanPostProcessor();
126+
}
127+
128+
}

0 commit comments

Comments
 (0)