A token base library, based on shiro to achieve login verification.
<dependency>
<groupId>com.facecto.code</groupId>
<artifactId>facecto-code-token-starter</artifactId>
<version>1.1.8</version>
</dependency>
app:
token:
key: String format. the key in redis.
secret: String format. the token secret. example: "3d15d32654bc1af61759a3bacbc0c78a"
expire: Integer format. the token expire time (seconds).
Note that the shiro function can only be used in the full mode.
import com.facecto.code.token.auth.AuthFilter;
import org.apache.shiro.mgt.SecurityManager;
import org.springframework.context.annotation.Bean;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.springframework.context.annotation.Configuration;
import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
@Configuration
public class ShiroConfig {
@Bean("shiroFilter")
public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
shiroFilter.setSecurityManager(securityManager);
Map<String, Filter> filters = new HashMap<>();
filters.put("oauth2", new AuthFilter());
shiroFilter.setFilters(filters);
Map<String, String> filterMap = new LinkedHashMap<>();
filterMap.put("/login", "anon");
filterMap.put("/**", "oauth2");
shiroFilter.setFilterChainDefinitionMap(filterMap);
return shiroFilter;
}
}
@EnableCodeToken
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
}
This component uses redis to access related authorization information. Redis must be introduced in the SpringBoot project.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
The default value of app.token.key is "adm-token". simple mode: If you don't use shiro's authorization and verification functions, you can use the simple mode. Simple mode allows you to use multiple custom keys in a project.
@Autowired
TokenHandler tokenHandler;
...
Token token = tokenHandler.createToken(user);
2020-02-14