-



Download 8,6 Mb.
Pdf ko'rish
bet7/37
Sana18.01.2022
Hajmi8,6 Mb.
#383795
1   2   3   4   5   6   7   8   9   10   ...   37
Bog'liq
Designing Applications with Spring Boot 2.2 and React JS Step-by-step guide to design and develop intuitive full stack web applications by Dinesh Rajput (z-lib.org)

ID = “resource
id”;
   @Override
   public void configure(ResourceServerSecurityConfigurer resources) {
   resources.resourceId(RESOURCE_ID).stateless(false);
   }
   @Override
   public void configure(HttpSecurity http) throws Exception {
   http.
   anonymous().disable()
   .authorizeRequests()
   .antMatchers(“/products/**”).access(“hasRole(‘USER’)”)
   .and().exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler());
   }
}
The preceding class is annotated with the @EnableResourceServer annotation. It is used for OAuth2 resource servers and it enables a Spring
Security filter that authenticates requests via an incoming OAuth2 token.
In our case, the resource application is an application which provides REST APIs to perform CRUD operations. But you need to pass the access
token to call the REST API of the resource server.
Let’s configure the Spring Security in the resource application using the following security configuration file:
package com.dineshonjava.prodos.security;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.oauth2.resource.
ResourceServerProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.
AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.
WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.oauth2.config.annotation.web.configuration.
EnableResourceServer;
import org.springframework.security.web.authentication. www.BasicAuthenticationFilter ;
@Configuration
@EnableResourceServer
@EnableWebSecurity
public class ResourceServerSecurityConfig extends WebSecurityConfigurerAdapter {
   @Autowired
   ResourceServerProperties resourceServerProperties;
   @Bean
   @Override
   public AuthenticationManager authenticationManagerBean() throws Exception {
   return super.authenticationManagerBean();
   }
   @Override
   protected void configure(HttpSecurity http) throws Exception {
   http
   .authorizeRequests()
   .anyRequest().authenticated()
   .and()
   .addFilterBefore(new OAuth2AuthenticationFilter(), BasicAuthenticationFilter.class)
   .csrf().disable()
   .httpBasic().disable()
   .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
   }
   @Autowired
   public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {


   auth.authenticationProvider(new OAuth2AuthenticationProvider(resourceServerProperties));
   }
}
As you can see in the preceding configuration file, we used a custom OAuth2AuthenticationFilter file to resolve the access token passed by the
client application. Let’s see the code of the custom filter:
package com.dineshonjava.prodos.security;
import java.io.IOException;
\import javax.servlet.FilterChain;
\import javax.servlet.ServletException;
\import javax.servlet.ServletRequest;
\import javax.servlet.ServletResponse;
\import javax.servlet.http.HttpServletRequest;
\import org.springframework.security.core.Authentication;
\import org.springframework.security.core.context.SecurityContextHolder;
\import org.springframework.util.StringUtils;
\import org.springframework.web.filter.GenericFilterBean;
public class OAuth2AuthenticationFilter extends GenericFilterBean {
   private static final String AUTHORIZATION = “Authorization”;
   private static final String PREFIX = “Bearer”;
   private static final String EMPTY = “”;
   @Override
   public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
   throws IOException, ServletException {
   String accessToken = ((HttpServletRequest) request).getHeader(AUTHORIZATION);
   if (accessToken != null && accessToken.startsWith(PREFIX)) {
   accessToken= accessToken.replace(PREFIX, EMPTY).trim();
   }
   if(!StringUtils.isEmpty(accessToken)) {
   Authentication auth = new OAuth2AuthenticationToken(accessToken);
   SecurityContextHolder.getContext().setAuthentication(auth);
   }
   filterChain.doFilter(request, response);
   }
}


The previous filter will be applied before calling the actual controller class, and this filter resolves the Authentication object from the access token
using the OAuth2AuthenticationToken class. We will also create a custom authentication provider to provide the authentication using the access
token, and this class will call the authorization server to validate the access token.
We will define the user info URI in the resource server application configuration file as shown in the following code:

Download 8,6 Mb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8   9   10   ...   37




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish