IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Spring Java Discussion :

Spring Security 404 Not Found


Sujet :

Spring Java

  1. #1
    Débutant  
    Profil pro
    Inscrit en
    Juin 2013
    Messages
    1 225
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2013
    Messages : 1 225
    Points : 132
    Points
    132
    Par défaut Spring Security 404 Not Found
    Bonjour,

    J'ai mi en place la sécurité dans mon application. Mon utilisateur est bien trouvé mais le retour me retourne un 404 au niveau de mon application : Not Found avec l'erreur suivante : Error: Request failed with status code 404
    Je veux que mon back me face pas de redirection mais qu'il me retourne juste mon objet User.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
     
    @SpringBootApplication
    @RestController
    @EnableWebSecurity
    public class CorsConfig implements WebMvcConfigurer {
    	private static Logger logger = LogManager.getLogger(CorsConfig.class);
    	// Match everything without a suffix (so not a static resource)
    	@RequestMapping(value = "/{path:[^\\.]*}")
    	public String redirect() {
    		// Forward to home page so that route is preserved.
    		return "forward:/";
    	}
     
    	@RequestMapping("/login")
    	@ResponseBody
    	public Principal user(HttpServletRequest request, Principal user) {
    		return user;
    	}
     
        @RequestMapping(value="/logout")
        public String logoutPage (HttpServletRequest request, HttpServletResponse response) {
            Authentication auth = SecurityContextHolder.getContext().getAuthentication();
            if (auth != null){  
             	logger.info("utilisateur déconnecté :"+auth.getName());
                new SecurityContextLogoutHandler().logout(request, response, auth);
     
            }
            return "redirect:/login?logout=true";
        }
     
     
    	public static void main(String[] args) {
    		// SpringApplication.run(UiApplication.class, args);
    		new SpringApplicationBuilder(CorsConfig.class).run(args);
    	}
     
    	@Configuration
    	@Order(SecurityProperties.BASIC_AUTH_ORDER)
    	protected static class ApplicationSecurity extends WebSecurityConfigurerAdapter {
    		@Autowired
    		private AppAuthenticationProvider appAuthenticationProvider;
     
    		public void setAppAuthenticationProvider(AppAuthenticationProvider appAuthenticationProvider) {
    			this.appAuthenticationProvider = appAuthenticationProvider;
    		}
     
     
    		@Override
    		protected void configure(HttpSecurity http) throws Exception {
    			  http.authorizeRequests()
                  .antMatchers("/mon-backend/**", "/lib/**", "/index.jsp", "/","/login")
                      .permitAll()
                  .antMatchers("/**")
                      .hasAnyRole("ADMIN", "USER")
                  .and()
                      .formLogin()
                      .loginPage("/login")
    //                  .defaultSuccessUrl("/home")
                      .failureUrl("/login?error=true")
                      .permitAll()
                  .and()
                      .logout()
                      .logoutSuccessUrl("/login?logout=true")
                      .invalidateHttpSession(true)
                      .permitAll()
                  .and()
                      .csrf()
                      .disable();
     
    		}
     
    		@Override
    		@Order(1)
    		public void configure(AuthenticationManagerBuilder auth) throws Exception {
    			auth.authenticationProvider(appAuthenticationProvider);
     
    		}
     
    	}
    }
    L'url d'accès au backend est http://localhost:8080/my-backend
    L'url d'accès au front est http://localhost:3000/

  2. #2
    Rédacteur/Modérateur
    Avatar de andry.aime
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    8 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Ile Maurice

    Informations forums :
    Inscription : Septembre 2007
    Messages : 8 391
    Points : 15 059
    Points
    15 059
    Par défaut
    Bonjour,

    Comment se communique les deux applications frontend et backend? Quel URL ne fonctionne pas? appelé depuis quel page?
    Commence d'abord par faire quelque chose de simple pour bien comprendre, une seule application pour le backend et frontend, intégré ensuite spring security.

    A+.

  3. #3
    Débutant  
    Profil pro
    Inscrit en
    Juin 2013
    Messages
    1 225
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2013
    Messages : 1 225
    Points : 132
    Points
    132
    Par défaut
    J''ai d'abord testé mon API avec Postman et cela ne fonctionne pas non plus.
    L'url http://localhost:8080/my-backend/login me retourne forwar:/ alors que je veux qu'elle me retourne mon objet user. Dans les logs je vois bien le traitement qui est fait, mon utilisateur est bien trouvé

  4. #4
    Rédacteur/Modérateur
    Avatar de andry.aime
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    8 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Ile Maurice

    Informations forums :
    Inscription : Septembre 2007
    Messages : 8 391
    Points : 15 059
    Points
    15 059
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    @RequestMapping(value = "/{[path:[^\\.]*}")
    public String redirect() {
      return "forward:/";
    }
    This method just needs to be in a @Controller (not a @RestController) somewhere in the Spring application.
    https://spring.io/blog/2015/05/13/mo...urity-part-vii

    Dans ton cas, tu l'as mis dans un @RestController or que ça devrait être dans un @Controller.

    A+.

  5. #5
    Débutant  
    Profil pro
    Inscrit en
    Juin 2013
    Messages
    1 225
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2013
    Messages : 1 225
    Points : 132
    Points
    132
    Par défaut
    Sa me retourne un statut code 200 et ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    <html>
        <body>
            <h2>Hello World!</h2>
        </body>
    </html>
    Mais comment récupérer mon user car là sa me le retourne toujours pas.

  6. #6
    Rédacteur/Modérateur
    Avatar de andry.aime
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    8 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Ile Maurice

    Informations forums :
    Inscription : Septembre 2007
    Messages : 8 391
    Points : 15 059
    Points
    15 059
    Par défaut
    Le @RequestMapping("/login") doit être dans une classe annotée @RestController, ou bien tu dois ajouter l'annotation @ResponseBody sur la méthode.

    A+.

  7. #7
    Débutant  
    Profil pro
    Inscrit en
    Juin 2013
    Messages
    1 225
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2013
    Messages : 1 225
    Points : 132
    Points
    132
    Par défaut
    Là je t'avoue je suis perdu complètement.
    Voici mon besoin : j'ai une application en REACT avec une API Rest. Je souhaite utilisé Spring.
    J’ai donc mi en place une page de loging qui une fois le formulaire validé on appel l'url http://localhost:8080/myBackend/login. Cette url doit utiliser Spring sécurité et me renvoyer mon user.
    Ensuite, à chaque nouvelle requête que je ferais, je devrais vérifier que mon suer est bien connecter pour accéder à la ressource demandé via Spring sécurité

Discussions similaires

  1. SPRING BOOT [ ERREUR ] État HTTP 404 – Not Found
    Par Oilag dans le forum Spring Boot
    Réponses: 2
    Dernier message: 18/07/2018, 12h14
  2. Echec de Post() avec IdHTTP et 404 not found
    Par randriano dans le forum Web & réseau
    Réponses: 3
    Dernier message: 20/04/2009, 15h49
  3. 404 not found
    Par monsieur77 dans le forum Réseau
    Réponses: 4
    Dernier message: 16/01/2008, 12h15
  4. [phpBB][3] Erreur 404 not found lors de la demande de la page d'installation
    Par juninho dans le forum EDI, CMS, Outils, Scripts et API
    Réponses: 14
    Dernier message: 12/01/2008, 19h49
  5. [etch] Pb "404 not found" avec Debmirror
    Par byloute dans le forum Debian
    Réponses: 0
    Dernier message: 31/10/2007, 10h56

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo