-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(scaffold): support jwt authentication with shiro
- Loading branch information
Showing
8 changed files
with
305 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const _ = require('lodash'); | ||
|
||
module.exports = { | ||
key: 'jwt', | ||
fn: { | ||
configureProviderPomDependencies (optionalDependencies, props) { | ||
optionalDependencies.push({ | ||
dependency: [ | ||
{ groupId: 'com.auth0' }, | ||
{ artifactId: 'java-jwt' }, | ||
{ version: '3.8.1' } | ||
] | ||
}) | ||
if (props.security === 'shiro') { | ||
optionalDependencies.push({ | ||
dependency: [ | ||
{ groupId: 'com.github.taccisum' }, | ||
{ artifactId: 'shiro-starter' }, | ||
{ version: '2.1.0' } | ||
] | ||
}) | ||
} | ||
}, | ||
configureApplicationYaml (yaml, env, props) { | ||
if (props.security === 'shiro') { | ||
switch (env) { | ||
case 'default': { | ||
_.merge(yaml, { | ||
shiro: { | ||
web: { | ||
mode: 'stateless', | ||
'filter-chain-definition': { | ||
authc: [ | ||
'/v1/**' | ||
], | ||
anon: [ | ||
'/**' | ||
] | ||
} | ||
} | ||
} | ||
}); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...tifactId}-provider/src/main/java/${basePath}/config/if_shiro.ShiroConfiguration.tmpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package ${basePackage}.config; | ||
|
||
<% | ||
switch (authentication) { | ||
case 'jwt': | ||
print(` | ||
import com.github.taccisum.shiro.web.autoconfigure.stateless.support.extractor.AuthorizationTokenExtractor; | ||
import com.github.taccisum.shiro.web.autoconfigure.stateless.support.extractor.TokenExtractor; | ||
import com.github.taccisum.shiro.web.autoconfigure.stateless.support.jwt.*; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.shiro.realm.Realm; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Slf4j | ||
@Configuration | ||
public class ShiroConfiguration { | ||
@Bean | ||
public Realm realm(JWTManager jwtManager) { | ||
return new OnlyParseJWTRealm("${jwtIssue}", jwtManager); | ||
} | ||
|
||
@Bean | ||
public PayloadTemplate payloadTemplate() { | ||
PayloadTemplate payloadTemplate = new DefaultPayloadTemplate("${jwtIssue}"); | ||
payloadTemplate.addField("userId", String.class); | ||
payloadTemplate.addField("accountId", String.class); | ||
payloadTemplate.addField("tenantId", String.class); | ||
return payloadTemplate; | ||
} | ||
|
||
@Bean | ||
public TokenExtractor tokenExtractor() { | ||
return new AuthorizationTokenExtractor(); | ||
} | ||
} | ||
`) | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
%> |
30 changes: 30 additions & 0 deletions
30
...artifactId}-provider/src/main/java/${basePath}/util/if_authentication.AuthUtils.tmpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package ${basePackage}.util; | ||
|
||
<% | ||
switch (authentication) { | ||
case 'jwt': | ||
print(` | ||
import com.github.taccisum.shiro.web.autoconfigure.stateless.support.jwt.JWTPrincipal; | ||
import com.github.taccisum.shiro.web.autoconfigure.stateless.support.jwt.Payload; | ||
import org.apache.shiro.SecurityUtils; | ||
|
||
public abstract class AuthUtils { | ||
public static String getToken() { | ||
return getPrincipal().getToken(); | ||
} | ||
|
||
public static Payload getPayload() { | ||
return getPrincipal().getPayload(); | ||
} | ||
|
||
private static JWTPrincipal getPrincipal() { | ||
return (JWTPrincipal) SecurityUtils.getSubject().getPrincipal(); | ||
} | ||
} | ||
`) | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
%> |
9 changes: 9 additions & 0 deletions
9
...ators/app/templates/${artifactId}-provider/src/main/resources/application.tmpl.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<% | ||
if(authentication !== 'none'){ | ||
print(` | ||
swagger.auth.api-key.token.keyName=Authorization | ||
swagger.auth.api-key.token.passAs=header | ||
swagger.auth.api-key.token.includePaths[0]=/** | ||
`) | ||
} | ||
%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters