Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring Security不阻止未经授权的用户登录_Java_Spring_Spring Mvc_Spring Security - Fatal编程技术网

Java Spring Security不阻止未经授权的用户登录

Java Spring Security不阻止未经授权的用户登录,java,spring,spring-mvc,spring-security,Java,Spring,Spring Mvc,Spring Security,我正在尝试学习Spring,并在我的应用程序中设置了Spring安全性。我只想允许数据库中的用户使用我的登录页面进行访问,该页面有一个简单的表单,应该是https,但目前https并没有被Spring强制执行,尽管它应该这样做,任何人都可以在我的应用程序中“登录”,并且在使用登录页面后不会收到任何错误 这是我的SecurityConfig.java页面: @Configuration @EnableWebSecurity public class SecurityConfig extends W

我正在尝试学习Spring,并在我的应用程序中设置了Spring安全性。我只想允许数据库中的用户使用我的登录页面进行访问,该页面有一个简单的表单,应该是https,但目前https并没有被Spring强制执行,尽管它应该这样做,任何人都可以在我的应用程序中“登录”,并且在使用登录页面后不会收到任何错误

这是我的SecurityConfig.java页面:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{

    @Autowired
    DataSource dataSource;

    @Override
    protected void configure(AuthenticationManagerBuilder auth)
    throws Exception {
    auth
    .jdbcAuthentication()
    .dataSource(dataSource)
    .usersByUsernameQuery(
            "select username, password" +
            "from users where username=?")
    .authoritiesByUsernameQuery(
    "select username from users where username=?")
    .passwordEncoder(new StandardPasswordEncoder("53cr3t"));
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception{
        http
        .formLogin()
        .loginPage("/Login.html")
        .and()
        .logout()
        .and()
        .authorizeRequests().antMatchers(HttpMethod.POST,"/Login").authenticated().
        anyRequest().authenticated()
        .and()
        .requiresChannel()
        .antMatchers("/Login").requiresSecure();
    }
}
这是我的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>MusicPortal</display-name>
 <servlet>
 <servlet-name>spring-dispatcher</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 </servlet>

 <servlet-mapping>
 <servlet-name>spring-dispatcher</servlet-name>
 <url-pattern>/</url-pattern>
  </servlet-mapping>

<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>



</web-app>

音乐门户
春季调度员
org.springframework.web.servlet.DispatcherServlet
春季调度员
/
springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
这是我的spring-dispatcher-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:security="http://www.springframework.org/schema/security"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"

    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-4.2.1.xsd">

<context:component-scan base-package="com.loucat.musicportal.controller,com.loucat.musicportal.model,com.loucat.musicportal.dao"/>


<bean id="viewResolver" class="org.thymeleaf.spring3.view.ThymeleafViewResolver" p:templateEngine-ref="templateEngine"/>
<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine" p:templateResolver-ref="templateResolver" />
<bean id="templateResolver"
      class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
      <property name="prefix" value="/WEB-INF/" />
  <property name="suffix" value=".html" />
  <property name="templateMode" value="HTML5" />
</bean> 



<bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url" value="jdbc:postgresql://localhost:5432/musicportal" />
        <property name="username" value="postgres" />
        <property name="password" value="postgres" />
    </bean>

<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />


</beans>

我的登录页面是Login.html,它有一个表单将结果发布到/PostLogin.html,后者有另一个控制器。 我想知道这是否是一个问题,因为在一些教程中,我看到他们使用登录页面本身

我希望这足以得到一些帮助,谢谢

尝试添加

<context:annotation-config/>


到您的spring dispatcher servlet.xml

SecurityConfig的包是什么?它在spring dispatcher servlet.xml中列出了吗?我想这就是问题所在:anyRequest().authenticated()@chomnoue:包没有列出,谢谢你发现了这一点。但是添加后行为没有改变…@StimpsonCat:你为什么认为这是个问题?我要求任何请求都经过身份验证,但没有一个是。。。我用错了吗?我用错了,但没什么变化。不确定是否值得一提。。。我的表单(Login.html)有一个post方法,它进入一个名为PostLogin.html的页面,该页面映射到一个控制器方法。这是否也是授权登录表单的正确方法?