Как интегрировать конфигурацию безопасности Spring в java и Spring mvc в xml - PullRequest
0 голосов
/ 02 марта 2020

Я пытаюсь настроить пружину MVC с xml и добавить защиту через java

spring-maven-config-servlet. xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!-- Add support for component scanning -->
    <context:component-scan base-package="com"/>

    <!-- Add support for conversion, formatting and validation support -->
    <mvc:annotation-driven/>

    <!-- Define Spring MVC view resolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"/>
        <property name="suffix" value=".jsp"></property>
    </bean> 

    <!-- Step 1: Define Database DataSource / connection pool -->
    <!--     c3p0 is mainly using for define connection pool how many connection define -->
    <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass"    value="oracle.jdbc.driver.OracleDriver"/>
        <property name="jdbcUrl"        value="jdbc:oracle:thin:@xyz/ora12cs1"/>
        <property name="user"           value="xyz"/>
        <property name="password"       value="xyz"/>
    </bean> 

     <!-- Step 2: Setup Hibernate session factory -->
     <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource"     ref="myDataSource"/>
        <property name="packagesToScan" value="com.rak.entities"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
     </bean>

     <!-- Step 3: Setup Hibernate transaction manager -->
     <bean id="myTransactionManager"
        class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
      </bean>

       <!-- Step 4: Enable configuration of transactional behavior based on annotations -->
    <tx:annotation-driven transaction-manager="myTransactionManager" />

    <!-- Add support for reading web resources: css, images, js, etc ... -->
<mvc:resources location="/resources/" mapping="/resources/**"></mvc:resources>

web. xml

 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
 http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Spring Maven</display-name>
  <absolute-ordering/>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-maven-config-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

Добавлена ​​конфигурация безопасности в java. Я добавил один пакет, добавил туда файл безопасности.

SecurityConfig. java

package com.rak.springSecurity;

import org.springframework.context.annotation.Configuration;
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.core.userdetails.User;
import org.springframework.security.core.userdetails.User.UserBuilder;


@Configuration
//we enable websecurity automaticlly by using these anotattion
@EnableWebSecurity

//WebSecurityConfigurerAdapter is a class contain in security it will provide enable security layer between http servelet call to our application

public class SecurityConfig extends WebSecurityConfigurerAdapter{

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        //user builder is class that we can create user and their role manually this is demo 
        UserBuilder users = User.withDefaultPasswordEncoder();
        auth.inMemoryAuthentication()
        .withUser(users.username("KRISHNA").password("123").roles("EMPLOYEE"))
        .withUser(users.username("RAKESH").password("123").roles("MANAGER"))
        .withUser(users.username("TINU").password("123").roles("CEO"));
    }

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

        http.authorizeRequests().anyRequest().authenticated().and()
        .formLogin().loginPage("/showLoginForm").loginProcessingUrl("/authenticateTheUser").permitAll();

    }



}

SecurityInitializer. java

package com.rak.springSecurity;

import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;

public class SecurityInitializer extends AbstractSecurityWebApplicationInitializer {

}

This is my package view

когда я так поступаю, это не фильтрует пружинную безопасность, пожалуйста, совет

...