【问题标题】:Spring MVC security not workingSpring MVC 安全性不起作用
【发布时间】:2014-12-30 11:37:33
【问题描述】:

我是 Spring mvc 的新手,我尝试遵循 http://docs.spring.io/spring-security/site/docs/3.2.5.RELEASE/reference/htmlsingle/#hello-web-security-java-configuration 中说明的安全示例

我已完成所有步骤,但没有发生任何事情,而不是在没有安全设置的情况下正常执行。该教程声称将首先显示登录页面。但那没有发生。我访问了系统,因为没有任何安全限制。

我的 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets 
        and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters, the 
        root application context is created by it -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

SecurityConfig.java

package com.channelit.example;

import org.springframework.beans.factory.annotation.Autowired;

    import org.springframework.context.annotation.*;
    import org.springframework.security.config.annotation.authentication.builders.*;
    import org.springframework.security.config.annotation.web.configuration.*;

    @Configuration
    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {

        @Autowired
        public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
            auth
                .inMemoryAuthentication()
                    .withUser("user").password("password").roles("USER");
        }
    }

【问题讨论】:

  • 您的安全控制码在哪里?
  • 我已经编辑了我的条目以包含我用于安全配置的 java 类。我是 Spring 新手,如果我不知道问题中包含什么内容,请原谅我
  • 这个链接绝对是不错的 Spring Security Starter - mkyong.com/tutorials/spring-security-tutorials
  • 好的,我发布了答案。

标签: java spring spring-mvc spring-security


【解决方案1】:

您错过了 SecurityConfig 类中的限制 url。

将此代码 sn-p 添加到您的 SecurityConfig 类中,如下所示:

    @Override
    protected void configure(HttpSecurity http) throws Exception {

      http.authorizeRequests()          
        .antMatchers("/your_restrict_url_here/**").access("hasRole('USER')")
        .and().formLogin();

    }

更多详情可以参考here.

【讨论】:

    猜你喜欢
    • 2015-06-20
    • 2011-07-03
    • 2014-06-14
    • 2019-08-12
    • 2014-07-25
    • 1970-01-01
    • 2019-12-18
    • 2021-12-15
    • 2016-12-06
    相关资源
    最近更新 更多