Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

老铁,Swagger2 怎么启用? #1

Closed
i-csharp opened this issue Sep 6, 2017 · 6 comments
Closed

老铁,Swagger2 怎么启用? #1

i-csharp opened this issue Sep 6, 2017 · 6 comments

Comments

@i-csharp
Copy link

i-csharp commented Sep 6, 2017

如题.
@configuration
@EnableSwagger2
启用Swagger2Config 后项目起不来了.

还有就是怎么全局保存用户登录信息呢.

@yujunhao8831
Copy link
Owner

aidijing-basic-config

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <!-- 注意这里 -->
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <!-- 注意这里 -->
            <scope>provided</scope>
        </dependency>

引入 aidijing-basic-config 时,并且需要启用 swagger ,那么源项目需要引入 swagger 的依赖
比如 :

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
        </dependency>

@i-csharp
Copy link
Author

i-csharp commented Sep 6, 2017

搞定了,谢谢大神.
麻烦再请教下全局保存用户登录信息呢.

@yujunhao8831
Copy link
Owner

ContextUtils 这里面获取用户信息
JWT 是无 session 化的,每次请求,请求端都需要携带token,然后解析token,再从redis中得到用户信息.

你看看 JwtAuthenticationTokenFilter.java

 @Override
    protected void doFilterInternal ( HttpServletRequest request , HttpServletResponse response ,
                                      FilterChain chain ) throws ServletException, IOException {
        final String authToken = this.extractAuthTokenFromRequest( request , this.tokenHeader );
        final String username  = jwtTokenUtil.getUsernameFromToken( authToken );

        LogUtils.getLogger().debug( "authToken : {},username : {}" , authToken , username );


        if ( username != null && SecurityContextHolder.getContext().getAuthentication() == null ) {
            // 对于简单的验证,只需检查令牌的完整性即可。 您不必强制调用数据库。 由你自己决定
            // 是否查询数据看情况,目前是查询数据库
            UserDetails userDetails = this.userDetailsService.loadUserByUsername( username );
            if ( jwtTokenUtil.validateToken( authToken , userDetails ) ) {
                UsernamePasswordAuthenticationToken authentication =
                    new UsernamePasswordAuthenticationToken( userDetails , null , userDetails.getAuthorities() );

                ThreadContext.put( USER_ID , String.valueOf( ( ( BasicJwtUser ) userDetails ).getId() ) );
                ThreadContext.put( USER_NAME , username );

                authentication.setDetails( new WebAuthenticationDetailsSource().buildDetails( request ) );

                LogUtils.getLogger().debug( "authToken : {},username : {}" , authToken , username );

                LogUtils.getLogger().debug( "该 " + username + "用户已认证, 设置安全上下文" );
                // 你看看这里的源码,设置到Security上下文中去了(当前请求有效)
                SecurityContextHolder.getContext().setAuthentication( authentication );
            }
        }
        chain.doFilter( request , response );
        ThreadContext.clearAll();
    }

ContextUtils.java 这个类的注释你也看看,这里就是从 SecurityContextHolder.getContext() 获取当次请求的用户信息

@i-csharp
Copy link
Author

i-csharp commented Sep 6, 2017

好的,谢谢大神,能不能建个群啊,方便大家交流.

@yujunhao8831
Copy link
Owner

抬举了,这个项目只是一些积累
群就不必了,比较麻烦. 这个项目不算复杂,看看就会了

@i-csharp
Copy link
Author

好的,谢谢大神.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants