顯示具有 oauth2 標籤的文章。 顯示所有文章
顯示具有 oauth2 標籤的文章。 顯示所有文章

2019年1月29日 星期二

OAuth2 Server實作,使用 Spring Boot

參考文件:
Spring OAuth 2 Developers Guide

如果你有很多系統但想用同一個方式登入,存取資源的話,建立 OAuth2 Server是一個好方法,可以參考 spring-security-oauth的方式來建立。 但目前很多範例是用 Spring Boot來建立,我參考的 Secure Spring REST With Spring Security and OAuth2 (github source code) 就是。
幾個點跟大家分享:


儲存 token, user帳號、宓碼會有三種方式
1.InMemory的方式,也是預設的方式

2.把資訊記錄在資料庫內,使用 jdbc data store,參考的範例內有 H2和 Postgresql,其它分享有用 MySQL的方式,如果是用 H2的話,一開始應該會放在本機,如果要用 remote模式要參考 Allow remote access to H2 Database設定,宓碼變更請參考 set password

3.jwt方式spring security 5.1開始就只支援這種方式


文章有提到會建立 Authorization Server, Resource Server,實際上是建立相關的 configuration class設定的動作,基本上有三個,也可以只寫一個 class extends 三個類別

2014年5月28日 星期三

Spring Security OAuth2 Client anonymous 存取 Oauth2 resource

spring-projects/spring-security-oauth 在這個專案有 OAuth1, OAuth2的 Server, Client實作,在 OAuth1 Client 存取 Server resource時是可以直接存取的,但在 OAuth2 Client的設計是要先登入才能存取 Server resource,如果要直接存取的話,則會出現錯誤如下

org.springframework.security.authentication.InsufficientAuthenticationException: Authentication is required to obtain an access token (anonymous not allowed)



這邊有一邊文章 (Integrating Google Calendar into a Wicket Application) 可以達到直接存取的目的,
作法大約如下:

1.實作一 class extends AbstractAuthenticationProcessingFilter, 最主要是在

  attemptAuthentication 內產生一個 TestingAuthenticationToken,只要是作為 anonymous登入用


   public class AnoAuthenicationProcessingFilter extends AbstractAuthenticationProcessingFilter {
private static java.util.logging.Logger log1 = java.util.logging.Logger.getLogger("");

    protected AnoAuthenicationProcessingFilter() {
    //super內的參數不可為空白
        super("/login.jsp");
    }

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException {
   
        //Authentication authentication = new TestingAuthenticationToken(request.getRemoteAddr(), request.getRemoteAddr(), "ROLE_ANONYMOUS");
   
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
   
    if ( authentication == null) {
            log1.log(Level.WARNING,".........establish a new tem Test token for "+ request.getRemoteAddr());
    authentication = new TestingAuthenticationToken(request.getRemoteAddr(), request.getRemoteAddr(), "ROLE_ANONYMOUS");
            authentication.setAuthenticated(true);
    }
   
   
        return getAuthenticationManager().authenticate(authentication);
     
    }

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
            throws IOException, ServletException {

        if (SecurityContextHolder.getContext().getAuthentication() == null) {
            SecurityContextHolder.getContext().setAuthentication(attemptAuthentication((HttpServletRequest) req, (HttpServletResponse) res));

            if (logger.isDebugEnabled()) {
                logger.debug("Populated SecurityContextHolder with dummy token: '"
                        + SecurityContextHolder.getContext().getAuthentication() + "'");
            }
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("SecurityContextHolder not populated with dummy token, as it already contained: '"
                        + SecurityContextHolder.getContext().getAuthentication() + "'");
            }
        }
   
        chain.doFilter(req, res);
    }
}

2.實作 implements AuthenticationProvider
    我希望使用者部份功能需要帳號密碼才能使用,所以在這邊有判斷 Authentication是那一種類
    別,在實作 support時就要加上可允許的 token類別

public class CattonOAuthClientProvider implements AuthenticationProvider{
private static java.util.logging.Logger log1 = java.util.logging.Logger.getLogger("");

public CattonOAuthClientProvider() {
super();
}

public Authentication authenticate(Authentication authentication)    throws AuthenticationException {
log1.log(Level.WARNING,"authentication class type............. " +authentication.getClass().toString());

        if (authentication instanceof UsernamePasswordAuthenticationToken)  {
        ‧‧‧‧‧
                ‧‧‧‧
        log1.log(Level.WARNING, "...login by account password...................");
        System.out.println("...login by account password...................");
       
       
        }else if (authentication instanceof TestingAuthenticationToken ) {
        log1.log(Level.WARNING, "...anonymous..............");
       
        } else {
        log1.log(Level.WARNING, "faile to authenicate in provder..............");
        }
       
     
       return authentication;
    }

     public boolean supports(Class authentication) {
          return TestingAuthenticationToken.class.isAssignableFrom(authentication)
       || UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication);

     }
}


 3.修改 spring-servlet.xml
 
    在 custom-filter after="EXCEPTION_TRANSLATION_FILTER" ref="oauth2ClientFilter" 之前加上
    custom-filter before="ANONYMOUS_FILTER" ref="authProcessingFilter"
 
    之後再加上
    bean class="com.catton.spring.security.AnoAuthenicationProcessingFilter" id="authProcessingFilter"
property name="authenticationManager" ref="defaultAuthenticationManager"

    bean id="authenticationProvider"   class="com.catton.spring.security.provider.CattonOAuthClientProvider"

    最後再修改 authentication-manager 設定為 authentication-provider ref="authenticationProvider" 即
    可